在下面的代码中,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()
语句,导致它突然工作
此外,由于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:")
如果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将永远不会捕获"0"的输入;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()