寶慕林4294392
2021-04-08 17:11:03
我試圖做while循環(huán)的三層嵌套。如果輸入一個十進(jìn)制數(shù)字,它將返回錯誤,然后,如果您輸入大于31的數(shù)字,它將返回錯誤,但是如果再次嘗試輸入十進(jìn)制數(shù)字,則代碼將停止。無論用戶輸入錯誤的格式次數(shù)或順序如何,都需要幫助使其無限期循環(huán)。還需要驗證輸入的日期對于給定月份中的天數(shù)有效嗎?import stringvarD= input("Enter Date/Day:")while varD.isdigit() or varD.isspace()\ or varD.isdecimal or int(varD)>31 \ or int(varD)==26 or int(varD)<=0: print ("Error: Enter Valid Number!") varD= input("Enter Day:")else: print ("You have entered:", varD)
1 回答

www說
TA貢獻(xiàn)1775條經(jīng)驗 獲得超8個贊
使用無限循環(huán)并僅在滿足所有條件時才中斷。
while True:
varD = input("Enter Day:")
if varD.isdigit() and not varD.isspace() and varD.isdecimal() \
and int(varD) < 32 and int(varD) != 26 and int(varD) > 0:
break
print("Error: Enter Valid Number!")
print("You have entered: %s" % varD)
同樣,您對術(shù)語三層嵌套的理解是不正確的。三層嵌套意味著這樣的事情:
while expression1:
while expression2:
while expression3:
do_something()
添加回答
舉報
0/150
提交
取消