Python 脚本中未达到 elif 语句
elif statement unreached in Python script
在下面的代码中,达到了 if
语句并且 运行 没问题。然而,elif
声明似乎没有任何效果。当脚本为 运行 时,当满足 elif
语句的条件时,什么也没有发生,当我按 return 按钮两次时,脚本只是继续跳过 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()
您必须在 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")
如果你想达到 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()
您需要将用户输入的初始 input()
移到 if
和 elif
语句之外,以便它们与相同的值进行比较。目前 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()
因为你从用户那里得到了一个输入并且在相同的范围内,如果这个输入量在 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()
代码中有太多不需要的 input()
语句,导致它突然工作。
Also, as python's input() function also has the capability of printing, you can combine print()
and input()
into one statement.
全部修改如下:
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:")
在下面的代码中,达到了 if
语句并且 运行 没问题。然而,elif
声明似乎没有任何效果。当脚本为 运行 时,当满足 elif
语句的条件时,什么也没有发生,当我按 return 按钮两次时,脚本只是继续跳过 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()
您必须在 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")
如果你想达到 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()
您需要将用户输入的初始 input()
移到 if
和 elif
语句之外,以便它们与相同的值进行比较。目前 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()
因为你从用户那里得到了一个输入并且在相同的范围内,如果这个输入量在 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()
代码中有太多不需要的 input()
语句,导致它突然工作。
Also, as python's input() function also has the capability of printing, you can combine
print()
andinput()
into one statement.
全部修改如下:
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:")