1 回答

TA貢獻1815條經驗 獲得超13個贊
lower是一個你需要像這樣調用的函數。您也不需要else在 while 循環中。
def hit_or_stay(deck,hand):
global playing
x = '' # just holds input for hit/stay
while x !='h' and x !='s':
try:
x = input('HIT or STAY? (h/s): ').lower()
except:
print("Please enter h to hit or s to stay." )
if x == 'h':
print("You have chosen to hit.")
hit(deck,hand)
elif x == 's':
print("You have chosen to stay.")
playing = False
else:
print(f"x equals {x}")
我不確定將try-except塊放在 while 循環中會產生什么行為。這行代碼可能拋出的唯一異常是用戶試圖通過按 Ctrl+C 退出程序。您的代碼會捕捉到這一點并繼續告訴用戶輸入 h 或 s。這通常不是好的行為——最好不要包含 try-except。
def hit_or_stay(deck,hand):
global playing
x = '' # just holds input for hit/stay
while x !='h' and x !='s':
x = input('HIT or STAY? (h/s): ').lower()
if x == 'h':
print("You have chosen to hit.")
hit(deck,hand)
elif x == 's':
print("You have chosen to stay.")
playing = False
else:
print(f"x equals {x}")
添加回答
舉報