Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 法定年龄证明人/审核人/计算器_Python_Python 3.x - Fatal编程技术网

Python 法定年龄证明人/审核人/计算器

Python 法定年龄证明人/审核人/计算器,python,python-3.x,Python,Python 3.x,我被要求使用用户的出生年份创建一个法定年龄验证程序。我已设法将用户的整数输入限制为4位,但它允许用户输入四位范围内的任何数字(0、234、1234等)。 如何强制输入4位数字并禁止低于1940的“输入” 我尝试了WHILE语句、IF语句和len()函数,但它们都不起作用 # Welcome user to the program greeting = "Hello! Welcome to the age calculator." print(greeting) print("\n") # I

我被要求使用用户的出生年份创建一个法定年龄验证程序。我已设法将用户的整数输入限制为4位,但它允许用户输入四位范围内的任何数字(0、234、1234等)。 如何强制输入4位数字并禁止低于1940的“输入”

我尝试了WHILE语句、IF语句和len()函数,但它们都不起作用

# Welcome user to the program
greeting = "Hello! Welcome to the age calculator."
print(greeting)

print("\n")

# Input current year
current_year = 2019
# Request the users year of birth and provide eg of birth year to guide user input to 4 digits
# Set integer digit limit to max 4 
birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])
# Use basic sum to calculate users age using this year 
age = current_year - birth_year
print(age)

# use IF statement to add legal age
if age >= 18:

print("Congrats you are old enough")

预期结果是1940年至当年之间的有效出生年份。

如评论中所述,这个问题不是很清楚。但是如果用户输入的值小于1940,我假设您希望继续请求用户输入其出生年份?如果需要更多条件,请改进您的问题

如果是这样,那么这就足够了:

birth\u year=0
出生年份<1940年时:
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
年龄=当前年份-出生年份
打印(您的年龄:{age})
如果年龄大于等于18岁:
打印(“恭喜你够大了”)

如果您希望对输入使用非常严格的规则,您可能希望使用正则表达式,这可以通过使用
regex
Python库来实现。

如注释中所述,问题不是很清楚。但是如果用户输入的值小于1940,我假设您希望继续请求用户输入其出生年份?如果需要更多条件,请改进您的问题

如果是这样,那么这就足够了:

birth\u year=0
出生年份<1940年时:
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
年龄=当前年份-出生年份
打印(您的年龄:{age})
如果年龄大于等于18岁:
打印(“恭喜你够大了”)

如果您希望输入有非常严格的规则,您可能希望使用正则表达式,这可以通过使用
regex
Python库来实现。

您可能还希望防止人们在当前年份之后输入年份。这将检查上限和下限。(也将使用

# Request the users year of birth and provide eg of birth year to guide user input to 4 digits
# Set integer digit limit to max 4 
birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])
while ((birth_year <= 1940) or (birth_year >= current_year)):
    print("Please try again")
    birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])

# Use basic sum to calculate users age using this year 
#请求用户的出生年份,并提供出生年份的eg,以引导用户输入4位数字
#将整数位数限制设置为最大4
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
而((出生年份=当前年份)):
打印(“请重试”)
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
#使用基本总和计算今年使用的用户年龄

您可能还希望阻止人们在当前年份之后进入年份。这将检查上限和下限。(也将使用

# Request the users year of birth and provide eg of birth year to guide user input to 4 digits
# Set integer digit limit to max 4 
birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])
while ((birth_year <= 1940) or (birth_year >= current_year)):
    print("Please try again")
    birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])

# Use basic sum to calculate users age using this year 
#请求用户的出生年份,并提供出生年份的eg,以引导用户输入4位数字
#将整数位数限制设置为最大4
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
而((出生年份=当前年份)):
打印(“请重试”)
出生年份=int(输入(“请确认您的出生年份”+“\n”+”例如:“1989”+“\n”)[:4])
#使用基本总和计算今年使用的用户年龄

您没有显示任何这些尝试,也没有显示“没有一个有效”的实际含义,因此不可能说出您做错了什么。请给出一个可能的副本。您没有显示任何这些尝试,也没有显示“没有一个有效”的实际含义,因此不可能说出您做错了什么。请给出一个可能的副本