【问题标题】:elif statement unreached in Python scriptPython 脚本中未达到 elif 语句
【发布时间】:2021-12-29 06:35:25
【问题描述】:

在下面的代码中,if 语句到达并运行良好。然而,elif 声明似乎没有任何效果。当脚本运行时,当满足elif 语句的条件时,什么也没有发生,当我按两次返回按钮时,脚本只是继续跳过elif 语句。

我的代码:

            print("If you want to name this window press 1, if you want to describe it press 2")
            if input() == "1":
                print("Please enter this window's title:")
                current_window_title = input()
                print("Do you also want to describe this window? P.S: You can do this later.)")
                if input().lower() == "yes":
                    print("Please enter this window's description:")
                    current_window_description = input()
                else:
                    current_window_description = "None"
            elif input() == "2":
                print("Please enter this window's description:")
                current_window_description = input()
                print("Do you also want to give this window a title? P.S: You can do this later.")
                if input().lower() == "yes":
                    print("Please enter this window's title:")
                    current_window_title = input()

【问题讨论】:

  • 您要求它输入两个不同的时间。对input() 的每次调用都希望您输入一个单独的号码。你需要写value = input()。然后if value == "1"if value == "2" 所以它不会尝试重新输入第二个数字。
  • elif 表示 else if,因此只有在前面的 if 条件为 False 时才会评估它 - 您是否首先输入了 '1' 以外的其他内容,然后 那么 '2'?但是,您可能希望将输入存储在变量中并在 if .. elif .. 语句中使用它
  • @FrankYellin 这很有意义并且有效,谢谢!

标签: python if-statement input user-input


【解决方案1】:

代码中有太多不想要的input() 语句,导致它突然工作。

另外,由于python的input()函数也有打印的能力,你可以将print()input()组合成一个语句。

所有修改如下:
Input = input("If you want to name this window press 1, if you want to describe it press 2")

if Input == "1":
    current_window_title = input("Please enter this window's title:")

    if input("Do you also want to describe this window? P.S: You can do this later.)").lower() == "yes":
        current_window_description = input("Please enter this window's description:")
    else:
        current_window_description = "None"
        

elif Input == "2":
    current_window_description = input("Please enter this window's description:")
    if input("Do you also want to give this window a title? P.S: You can do this later.").lower() == "yes":
        current_window_title = input("Please enter this window's title:")

【讨论】:

    【解决方案2】:

    你必须在 if 之前使用输入变量 elif else 阻塞。

    answer=input("If you want to name this window press 1, if you want to describe it press 2:")
                if answer == "1":
                   Code section 1
               elif answer=="2":
                   Code section 2
               else : 
                  print ("wrong command")
    

    【讨论】:

      【解决方案3】:

      如果你想达到 2,你的代码会要求输入两次。第一次,它会要求输入,它在 if 语句中,你会输入 2,它不会匹配。第二次它会要求输入,你在 elif 语句中,它会工作。

      最好先得到输入的结果,然后再进行评估。

      print("If you want to name this window press 1, if you want to describe it press 2")
      result = input()
      if result == "1":
          print("Please enter this window's title:")
          current_window_title = input()
          print("Do you also want to describe this window? P.S: You can do this later.)")
          if input().lower() == "yes":
              print("Please enter this window's description:")
              current_window_description = input()
          else:
              current_window_description = "None"
      elif result == "2":
          print("Please enter this window's description:")
          current_window_description = input()
          print("Do you also want to give this window a title? P.S: You can do this later.")
          if input().lower() == "yes":
              print("Please enter this window's title:")
              current_window_title = input()
      

      【讨论】:

        【解决方案4】:

        您需要将用户输入的初始 input() 移到 ifelif 语句之外,以便它们与相同的值进行比较。目前,elif 永远不会捕获“2”的输入,除非您输入第二个输入(再次输入“2”)。试试这样的。

        print("If you want to name this window press 1, if you want to describe it press 2")
        user_input = input()
        if user_input == "1":
            print("Please enter this window's title:")
            current_window_title = input()
            print("Do you also want to describe this window? P.S: You can do this later.)")
            if input().lower() == "yes":
                print("Please enter this window's description:")
                current_window_description = input()
            else:
                current_window_description = "None"
        elif user_input == "2":
            print("Please enter this window's description:")
            current_window_description = input()
            print("Do you also want to give this window a title? P.S: You can do this later.")
            if input().lower() == "yes":
                print("Please enter this window's title:")
                current_window_title = input()
        

        【讨论】:

          【解决方案5】:

          因为你从用户那里得到了一个输入并且在相同的范围内,如果这个输入量在 elif 中不可用,你可以做这两件事,它会正常工作:

          print("If you want to name this window press 1, if you want to describe it press 2")
          inp = input()
          if inp == "1":
              print("Please enter this window's title:")
              current_window_title = input()
              print("Do you also want to describe this window? P.S: You can do this later.)")
              if input().lower() == "yes":
                  print("Please enter this window's description:")
                  current_window_description = input()
              else:
                  current_window_description = "None"
          elif inp == "2":
              print("Please enter this window's description:")
              current_window_description = input()
              print("Do you also want to give this window a title? P.S: You can do this later.")
              if input().lower() == "yes":
                  print("Please enter this window's title:")
                  current_window_title = input()
          

          或:

          print("If you want to name this window press 1, if you want to describe it press 2")
          if input() == "1":
              print("Please enter this window's title:")
              current_window_title = input()
              print("Do you also want to describe this window? P.S: You can do this later.)")
              if input().lower() == "yes":
                  print("Please enter this window's description:")
                  current_window_description = input()
              else:
                  current_window_description = "None"
          if input() == "2":
              print("Please enter this window's description:")
              current_window_description = input()
              print("Do you also want to give this window a title? P.S: You can do this later.")
              if input().lower() == "yes":
                  print("Please enter this window's title:")
                  current_window_title = input()
          

          【讨论】:

          • 这段代码还是存在输入语句冗余的问题,会导致行为突兀
          • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-23
          • 2020-09-23
          • 1970-01-01
          • 2016-09-04
          • 2021-12-10
          • 2020-10-28
          • 2015-01-16
          相关资源
          最近更新 更多