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}")
添加回答
举报