【问题标题】:How do I get my (multiple) conditions to be met?如何满足我的(多个)条件?
【发布时间】:2021-10-12 05:06:51
【问题描述】:

所以最近我刚刚使用 if else 语句进行了一项调查,并遇到了我的条件未完全满足的问题。起初,我的代码似乎运行良好,但后来我意识到,在某些输入下,代码会崩溃或响应不同的条件。

我最大的问题是,当我在(2 到 10)中输入任何内容时,它指的是只有在输入大于 18 时才可能出现的条件......但是数字(10 到 17)在什么情况下工作得很好我需要

同样,对于任何 100 及以上的数字,它都不是指 >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!")

【问题讨论】:

    标签: if-statement python-3.9 multiple-conditions


    【解决方案1】:

    您已输入字符串并将其与str(18) 进行比较。这是比较两个字符串而不是两个数字。因此,当您执行"9" &lt; "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!")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2021-06-14
      • 2013-10-14
      相关资源
      最近更新 更多