def interact():
while True:
try:
num = int(input("Please input an integer: "))
if (num % 2) == 0:
print ("{0} is even".format(num))
else:
print("{0} is odd".format(num))
num_two = int(input('Do you want to play again n/Y:'))
except:
if num_two == "y":
continue
finally:
print("Goodbye")
break
uj5u.com熱心網友回復:
def interact():
while True:
try:
num = int(input("Please input an integer: "))
if (num % 2) == 0: print ("{0} is even".format(num))
else: print("{0} is odd".format(num))
num_two = int(input('Do you want to play again n/Y:'))
except:
if num_two == "y":
continue
finally:
print("Goodbye")
break
這段代碼的問題在于“中斷”使它脫離了 while true 回圈(我認為)
因此我認為洗掉“休息”可能會解決它?
uj5u.com熱心網友回復:
您必須將特定錯誤添加到 except 并從 finally 中洗掉中斷:
try:
user_input = input()
int(user_input)
except ValueError:
print('not a number')
if user_input = 'y':
continue
else:
break
finally:
print('Finnaly')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386310.html
