【问题标题】:How can i get my code to work correctly? user input is not working with if, elif, and else我怎样才能让我的代码正常工作?用户输入不适用于 if、elif 和 else
【发布时间】:2021-05-10 02:31:11
【问题描述】:

if、elif 和 else 无法正确处理我的输入。我正在努力做到这一点,所以你必须通过所有三个问题才能进入我的假俱乐部。但问题是,如果您错误地回答了前两个问题而正确回答了第三个问题,您仍然可以进入俱乐部。另外,这是学校的作业,我的老师说我错过了一个标题,我的 if 语句不包含实际淘汰或接受潜在用户的逻辑语句。请帮忙。

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()

【问题讨论】:

    标签: python if-statement input


    【解决方案1】:

    原因是你说只有( cool == "i have mixed feelings") 然后显示你被允许加入"cool kids"。 你也可以使用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()
    

    【讨论】:

      【解决方案2】:

      最后一个 if 与 else 语句相关联,所以即使前两个是错误的,最后一个是正确的,它也会允许你输入。更好的逻辑是为每个要求设置条件。

      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()
      

      虽然这会导致代码更冗长,但它可以帮助您更好地了解逻辑

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-30
        • 2023-03-16
        • 2020-10-24
        • 1970-01-01
        • 2022-07-08
        • 2019-06-19
        • 1970-01-01
        • 2021-06-06
        相关资源
        最近更新 更多