我试图用Python编程21点牌游戏。 在玩家类中,我想定义一个循环,要求玩家决定是“打”还是“站”(二十一点规则)。除非输入是正确的(无论是“S”还是“H”),循环需要循环,直到玩家输入其中一个选项。错误处理所需的输入
这是我为这个特定的部分代码:
while True:
try:
D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')
if D in ['S', 'H'] is False:
1/0
except:
print('Incorrect input, please try again (S for stand and H for hit)!')
continue
else:
if D == 'S':
print('OK, you decided to stand!')
else:
print('OK, you decided to hit. You will receive a 3rd card!')
break
这样的想法是,除非决策是正确的(“S”或“H”),将创建一个错误,但到目前为止,代码无法正常工作呢...我认为有一个小毛刺......
任何建议? 亲切的问候,
大号
你知道吗,例如['S','H']中的'Foo'是False'评估结果?它可能让你感到惊讶。 – jonrsharpe
除了以外,你还期待什么?你应该期待尝试除块 –