Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要输入错误处理_Python_Python 3.x_Error Handling - Fatal编程技术网

Python 需要输入错误处理

Python 需要输入错误处理,python,python-3.x,error-handling,Python,Python 3.x,Error Handling,我正在尝试用Python编写21点纸牌游戏。 在player类中,我想定义一个循环,要求玩家决定“击中”或“站立”(21点规则)。除非输入正确(站立时为“S”,命中时为“H”),否则循环需要循环,直到玩家输入这两个选项中的一个 以下是我针对此特定部分的代码: while True: try: D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')

我正在尝试用Python编写21点纸牌游戏。 在player类中,我想定义一个循环,要求玩家决定“击中”或“站立”(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'),否则会产生一个错误,但到目前为止,代码还不能正常工作。。。我认为有一个小问题

有什么建议吗? 亲切问候,

你应该写:

if D not in ['S', 'H']:
整个代码将更加简短,可读性更强,毫无例外:

while True:
    D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')
    if D not in ['S', 'H']:
        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 
你应该写:

if D not in ['S', 'H']:
整个代码将更加简短,可读性更强,毫无例外:

while True:
    D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')
    if D not in ['S', 'H']:
        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 

无需例外,您只需执行以下操作:

while True:    # infinite loop
    D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')
    if D == "S":
        #do some
        break 
    elif D == "H":
        # Hit some.
        break
    else:
        print('Incorrect input, please try again (S for stand and H for hit)!')
        break

无需例外,您只需执行以下操作:

while True:    # infinite loop
    D = input('What is your decision, stand or hit? [press S for stand and H for hit]: ')
    if D == "S":
        #do some
        break 
    elif D == "H":
        # Hit some.
        break
    else:
        print('Incorrect input, please try again (S for stand and H for hit)!')
        break

你知道['S',H']中的'Foo'是假的吗?这可能会让你大吃一惊。你在期待什么呢?你应该在try中期待一些东西,除了block之外。你知道['S','H']中的
'Foo'是假的吗?这可能会让你大吃一惊。你在期待什么呢?你应该期待一些东西在尝试除了谢谢你,这是更干净@麦克卢卡,不客气!不要担心被否决的选票。。。有时你可以控制人们的想法,即使他们是wrong@mcluka别担心,有时候我也不明白,但当我看到类似的东西时,我会尝试修复它,所以我把你的问题投了赞成票,因为这是一个非常好的问题,有例子,简短的代码示例,显示出努力,我的意思是,它拥有一切。。。我很高兴我能帮助你,别忘了接受如果有帮助的话,我一直很乐意帮助你。谢谢你,这更干净了@麦克卢卡,不客气!不要担心被否决的选票。。。有时你可以控制人们的想法,即使他们是wrong@mcluka别担心,有时候我也不明白,但当我看到类似的东西时,我会尝试修复它,所以我把你的问题投了赞成票,因为这是一个非常好的问题,有例子,简短的代码示例,显示出努力,我的意思是,它拥有一切。。。我很高兴我能帮助你,如果有帮助的话,别忘了接受,我一直很乐意帮助你