如何满足我的(多个)条件?
How do I get my (multiple) conditions to be met?
所以最近我一直在用 if else 语句和 运行 调查我的条件没有完全满足的问题。起初我的代码似乎 运行 很好,但后来我意识到,对于某些输入,代码会崩溃或响应不同的条件。
我最大的问题是当我输入(2 到 10)中的任何一个时,它指的是只有当输入大于 18 时才可能出现的条件...但是数字(10 到 17)工作得很好我需要的
同样,对于任何 100 及以上的数字,它都不是指 >18 代码,而是指小于 <18
我觉得我错过了什么,下面是代码,请帮助
print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
"DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()
if age >= str(18) and gender == Male:
print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
input("Name and DOB: ")
print("You will get a letter/email if your service is ever required.")
if age < str(18)) and gender == Male:
print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))
else:
if gender == Female:
print("You do not meet the criteria to sign up, have a good day!")
您已获取字符串输入并将其与 str(18)
进行比较。这是比较两个字符串而不是两个数字。所以当你做类似 "9" < "18"
的事情时,你实际上是在比较它们的 Unicode 值 而不是数字本身。因此,将整数转换为字符串会将您的输入字符串转换为整数,因此您的代码应该是这样的:
print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
"DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()
if int(age) >= 18 and gender == Male:
print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
input("Name and DOB: ")
print("You will get a letter/email if your service is ever required.")
if int(age) < 18 and gender == Male:
print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))
else:
if gender == Female:
print("You do not meet the criteria to sign up, have a good day!")
所以最近我一直在用 if else 语句和 运行 调查我的条件没有完全满足的问题。起初我的代码似乎 运行 很好,但后来我意识到,对于某些输入,代码会崩溃或响应不同的条件。
我最大的问题是当我输入(2 到 10)中的任何一个时,它指的是只有当输入大于 18 时才可能出现的条件...但是数字(10 到 17)工作得很好我需要的
同样,对于任何 100 及以上的数字,它都不是指 >18 代码,而是指小于 <18
我觉得我错过了什么,下面是代码,请帮助
print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
"DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()
if age >= str(18) and gender == Male:
print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
input("Name and DOB: ")
print("You will get a letter/email if your service is ever required.")
if age < str(18)) and gender == Male:
print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))
else:
if gender == Female:
print("You do not meet the criteria to sign up, have a good day!")
您已获取字符串输入并将其与 str(18)
进行比较。这是比较两个字符串而不是两个数字。所以当你做类似 "9" < "18"
的事情时,你实际上是在比较它们的 Unicode 值 而不是数字本身。因此,将整数转换为字符串会将您的输入字符串转换为整数,因此您的代码应该是这样的:
print("Welcome user, today we will be evaluating if you need to sign up for selective service or not.\n"
"DO NOT LIE!")
print("")
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Male = "M".casefold()
Female = "F".casefold()
gender = input("Please enter an (M or F) for gender identity: ").casefold()
if int(age) >= 18 and gender == Male:
print("Welcome {}, since you are a Male that is 18 or over please sign where directed for selective service".format(name))
input("Name and DOB: ")
print("You will get a letter/email if your service is ever required.")
if int(age) < 18 and gender == Male:
print("You are not required to signup for selective service yet {}, please return in {} years.".format(name, 18-int(age)))
else:
if gender == Female:
print("You do not meet the criteria to sign up, have a good day!")