简体   繁体   English

我怎样才能让我的代码正常工作? 用户输入不适用于 if、elif 和 else

[英]How can i get my code to work correctly? user input is not working with if, elif, and else

if, elif, and else are not working with my inputs correctly. if、elif 和 else 无法正确处理我的输入。 I am trying to make it so you have to pass all three questions to gain access to my fake club.我正在努力做到这一点,所以你必须通过所有三个问题才能进入我的假俱乐部。 but the problem is if you answer the first two questions incorrectly and the third one correctly, you still gain access to the club.但问题是,如果您错误地回答了前两个问题而正确回答了第三个问题,您仍然可以进入俱乐部。 also, this is an assignment for school, and my teacher said i was missing a heading, and my if statements contained no logical statements to actually weed out or accept potential users.另外,这是学校的作业,我的老师说我错过了一个标题,我的 if 语句不包含实际淘汰或接受潜在用户的逻辑语句。 please, help.请帮忙。

def main():
    age = input("are you younger than 21?")
    game = input("what is the game of the day?")
    cool = input("what do you think about school?") 
    
    print("Welcome to Kidz Only.")
    print("are you cool enough to enter?")
    
    if( age == "yes"):
        print("question one is done.")
    if ( game == "super mario bros"):
        print("question two is through.")    
    if ( cool == "i have mixed feelings"):
        print("question three is complete.")
        print("You have passed the quiz. Welcome to Kidz Only!")
        
    else:
        print("Sorry, but you may not enter Kidz Only.")
        
main()

Reason to that would be because you are saying only if ( cool == "i have mixed feelings") then display you are allowed to join the "cool kids" .这样做的原因是因为你说只有当( cool == "i have mixed feelings")然后显示你被允许加入"cool kids" You can also shorten your code by using and .您还可以使用and来缩短代码。

def main():
    age = input("are you younger than 21?")
    game = input("what is the game of the day?")
    cool = input("what do you think about school?") 
    
    print("Welcome to Kidz Only.")
    print("are you cool enough to enter?")
    
    if( age == "yes") and (game == "super mario bros") and ( cool == "i have mixed feelings"):
        print("question three is complete.")
        print("You have passed the quiz. Welcome to Kidz Only!")
        
    else:
        print("Sorry, but you may not enter Kidz Only.")
        
main()

The last if is tied to the else statement so even if the first two are wrong and the last one is correct, it will allow you to enter.最后一个 if 与 else 语句相关联,因此即使前两个错误而最后一个正确,它也允许您输入。 A better logic would be to have conditionals for each of those requirements.更好的逻辑是为每个要求设置条件。

def main():
    age = input("are you younger than 21?")
    game = input("what is the game of the day?")
    cool = input("what do you think about school?") 
    
    print("Welcome to Kidz Only.")
    print("are you cool enough to enter?")
    
    hasAge = False
    hasGame = False
    hasCool = False

    if( age == "yes"):
        hasAge = True
        print("question one is done.")
    if ( game == "super mario bros"):
        hasGame = True
        print("question two is through.")    
    if ( cool == "i have mixed feelings"):
        hasCool = True
        print("question three is complete.")
        
    if (hasAge and hasGame and hasCool):
        print("You have passed the quiz. Welcome to Kidz Only!")
    else:
        print("Sorry, but you may not enter Kidz Only.")
        
main()

Although this results in a more lengthy code, it helps you see the logic better虽然这会导致代码更冗长,但它可以帮助您更好地查看逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM