Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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 2.7.8如何重新启动游戏代码_Python - Fatal编程技术网

Python 2.7.8如何重新启动游戏代码

Python 2.7.8如何重新启动游戏代码,python,Python,我在看如何重新启动这个代码,而不实际重新启动游戏。我希望能够有游戏重新启动其上 print 'Welcome to the game of NIM: ' player=1 state=21 print 'The number of objects is now ',state while True: print 'Player ',player while True: move=input('What is your move? ') if mov

我在看如何重新启动这个代码,而不实际重新启动游戏。我希望能够有游戏重新启动其上

print 'Welcome to the game of NIM: '
player=1
state=21
print 'The number of objects is now ',state
while True:
    print 'Player ',player
    while True:
        move=input('What is your move? ')
        if move in [1,2,3] and move<state:
            break
        print 'Illegal move.'
    state=state-move
    print 'The number of objects is now ',state

    if state==1:
        print 'Player ',player,' wins!'
        break
    if player==1:
        player=2
    else:
        player=1
print 'Game is over.'
print'欢迎来到NIM的游戏:'
玩家=1
州=21
打印“现在对象的数量”,状态为
尽管如此:
打印“播放器”,播放器
尽管如此:
移动=输入('你的移动是什么?')

如果移入[1,2,3]并移动,则可以将代码放入函数中,并在while循环中调用该函数:

from sys import exit #To exit if the user does not want to try again
print 'Welcome to the game of NIM: '
def game():
    player=1
    state=21
    print 'The number of objects is now ',state
    while True:
        print 'Player ',player
        while True:
            move=input('What is your move? ')
            if move in [1,2,3] and move<state:
                break
            print 'Illegal move.'
        state=state-move
        print 'The number of objects is now ',state

        if state==1:
            print 'Player ',player,' wins!'
            break
        if player==1:
            player=2
        else:
            player=1
    if raw_input('Would you like to try again? ').lower().startswith('y'):
        return #To exit from the function cleanly
    exit() #To exit from the program cleanly if the user does not want to try again
def run():
    while True:
        game()
if __name__ == '__main__':
    run()
from sys import exit#如果用户不想重试,则退出
打印“欢迎来到尼姆的游戏:”
def game():
玩家=1
州=21
打印“现在对象的数量”,状态为
尽管如此:
打印“播放器”,播放器
尽管如此:
移动=输入('你的移动是什么?')

如果移入[1,2,3]和移动如何准确退出?我刚刚编辑了代码,现在,程序将询问用户是否想重试,如果他们想重试,它将重新启动,如果不想,它将使用我添加的链接退出。