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_input == "y":
continue
finally:
print("Goodbye")
print(num_two)
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))
except:
continue
if input("Would you like to play again? Y/N: ").lower() == "y":
continue
else:
print("goodbye")
break
我相信這會產生你想要的效果。
uj5u.com熱心網友回復:
play-again 提示需要輸入一個字串,而不是一個數字。不要試圖把它當作一個數字。將try陳述句限制為測驗數字輸入:如果得到 a ValueError,則立即重新啟動回圈,而不是嘗試測驗它是偶數還是奇數。
當您再次播放回應時,break如果回應不是,則使用退出回圈y。
回圈退出后列印“再見”。
def interact():
while True:
try:
num = int(input("Please input an integer: "))
except ValueError:
continue # Ask for a number again
if num % 2 == 0:
print("{0} is even".format(num))
else:
print("{0} is odd".format(num))
response = input("Play again? (n/Y) ")
if response != "y":
break
print("Good bye")
uj5u.com熱心網友回復:
更緊湊的方法:
def interact():
want_continue = "Y"
while want_continue == "Y":
try:
num = int(input("Please input an integer: "))
print ("{0} is {1}".format(num, "even" if num % 2 == 0 else "odd"))
want_continue = input('Do you want to play again (n/Y):').upper()
except ValueError:
print("Please enter a number")
print("Goodbye")
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_input == "y":
continue
finally:
print("Goodbye")
break
print(num_two)
uj5u.com熱心網友回復:
我認為代碼應該是這樣的:
def interact():
try:
while True:
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 = str(input('Do you want to play again n/Y:'))
if num_two != "y":
raise
except:
print("Goodbye")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386308.html
