我有這個代碼
n,h,y=input("Enter three values: ").split(" ")
如果用戶輸入 1 個值或 2 個值,則會出現錯誤(沒有足夠的值來解包(預期為 3,得到 1)如果用戶犯此錯誤,如何重新輸入?使用 while True 和 if 陳述句?。
uj5u.com熱心網友回復:
?您可以使用以下代碼:
n, *others = input("Enter three values: ").split(" ")
在上面的代碼中,基于輸入數字的數量others是一個包含零到無限數字的串列。例如,如果你寫1 10 2. others是一個有[10, 2]值的串列,如果你只寫一個數字1,others就是一個空串列。
uj5u.com熱心網友回復:
def take3Input():
Loop = True
while Loop:
try:
n,h,y=input("Enter three values: ").split(" ")
Loop =False
except:
print("please input 3 values")
take3Input()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371027.html
