while True:
a = int(input("Enter a number 1: "))
if a in range(121):
pass
else:
print("Out of Range")
b = int(input("Enter a number 2: "))
if b in range(121):
pass
else:
print("Out of Range")
c =int(input("Enter a number 3: "))
if c in range(121):
pass
else:
print("Out of Range")
if a b c == 120:
break
else:
print("Incorrect Total")
我想檢查輸入數字是否在范圍內,如果沒有再次獲得相同的數字,最后檢查 3 個變數輸入是否加起來為 120,如果沒有再次要求輸入 3 個數字,但在上面的代碼中我無法得到它在超出范圍時要求相同的數字。
uj5u.com熱心網友回復:
你的想法是對的,但回圈有點混亂。這將一直詢問,直到您輸入“好”值。如果總數不正確,它將根據要求重新啟動。
def get_input(n):
inp = -1
while inp not in range(121):
inp = int(input(f"Enter a number {n}: ")) # f-strings require python >= 3.6
return inp
while True:
a = get_input(1)
b = get_input(2)
c = get_input(3)
if a b c == 120:
break
else:
print("Incorrect Total")
uj5u.com熱心網友回復:
您需要回圈輸入呼叫
while True:
a = int(input("Enter a number 1: "))
if a in range(121):
break
else:
print("Out of Range")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/372026.html
下一篇:如何在資料網格視圖中顯示資料
