1 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
lower是一個(gè)你需要像這樣調(diào)用的函數(shù)。您也不需要else在 while 循環(huán)中。
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 循環(huán)中會產(chǎn)生什么行為。這行代碼可能拋出的唯一異常是用戶試圖通過按 Ctrl+C 退出程序。您的代碼會捕捉到這一點(diǎn)并繼續(xù)告訴用戶輸入 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}")
添加回答
舉報(bào)