在以下代碼中,該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()
uj5u.com熱心網友回復:
代碼中有太多不需要的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:")
uj5u.com熱心網友回復:
您必須在 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")
uj5u.com熱心網友回復:
如果你想達到 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()
uj5u.com熱心網友回復:
您需要將input()用戶輸入的首字母移到ifandelif陳述句之外,以便它們與相同的值進行比較。目前 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()
uj5u.com熱心網友回復:
因為你從用戶那里得到了一個輸入并且在相同的范圍內,如果 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/397558.html
