這是我到目前為止:userNum = int(input('How many perfect numbers do you wish to sum? (1-4)'))while userNum != 1 and userNum != 2 and userNum != 3 and userNum != 4: userNum = input('You did not enter a number 1-4! Try again!')if userNum == 1: print('Sum is 6 = 6')elif userNum == 2: print('Sum is 6 + 28 = 34')elif userNum == 3: print('Sum is 6 + 28 + 496 = 530')elif userNum == 4: print('Sum is 6 + 28 + 496 + 8128 = 8658')如果我輸入的數(shù)字不是 1、2、3 或 4,它會重新提示,但如果我輸入指定的整數(shù)之一,它不會退出 while 循環(huán)。有什么建議嗎?
1 回答

慕蓋茨4494581
TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊
input 正在返回一個字符串,您正在將它與一個 int 進(jìn)行比較。
userInput = 0
while userInput not in [1, 2, 3, 4]:
userInput = int(input('Enter a number'))
添加回答
舉報
0/150
提交
取消