Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 2.7_Loops_While Loop_Infinite Loop - Fatal编程技术网

Python 如何排除无限循环故障

Python 如何排除无限循环故障,python,python-2.7,loops,while-loop,infinite-loop,Python,Python 2.7,Loops,While Loop,Infinite Loop,我正在写一个棍棒游戏,希望能和我比赛。这个想法在中有详细介绍,但基本上我有代码,可以创建一种非常基本的人工智能类型,可以自适应地学习最佳的游戏方式 我有玩家1和AI的代码,但问题是我需要AI持续运行,直到我告诉它停止。我尝试过在一段时间内将所有代码封装为True:但我认为条件语句中的某些内容导致代码停止,而不是不断循环。我希望有人能对我如何实施变通方案提出建议 import random import sys "Global Variables" prompt = '>>>

我正在写一个棍棒游戏,希望能和我比赛。这个想法在中有详细介绍,但基本上我有代码,可以创建一种非常基本的人工智能类型,可以自适应地学习最佳的游戏方式

我有玩家1和AI的代码,但问题是我需要AI持续运行,直到我告诉它停止。我尝试过在一段时间内将所有代码封装为True:但我认为条件语句中的某些内容导致代码停止,而不是不断循环。我希望有人能对我如何实施变通方案提出建议

import random
import sys

"Global Variables"
prompt = '>>> ' 
btw_1_3 = 'Please pick a number beteween 1 and 3.'


#Introduction to the game
raw_input("Welcome to the game of sticks! Press <Enter> to continue...")
print("How many sticks are there on the table initially (10-100)?")
num_sticks = int(raw_input(prompt))
print("Ok, there are %s sticks on the board.") % (num_sticks)
print("|")*num_sticks

#Initialize a dictionary that we will change
stick_dict={}
for number in range(1,num_sticks+1):
    stick_dict["hat{0}".format(number)]=[[1,2,3]]



while True:
    while num_sticks!=0:

        #player 1
        while True:
            print "Player 1: How many sticks do you take (1-3)?"
            player_1 =  int(raw_input(prompt))

            if 1 <= player_1 <= 3:
                break
            else:
                print (btw_1_3)

        num_sticks = num_sticks - player_1

        if num_sticks == 1:
            print("Ok, there is %s stick on the board.") % (num_sticks)
            print("|")*num_sticks

        elif num_sticks < 1:
            print("Player 1, you lose.")

            #If Player 1 loses, then the AI wins and we want to execute this 
            for key in stick_dict:
                if len(stick_dict[key]) == 2:
                    stick_dict[key][0].append(stick_dict[key][1])
                    del stick_dict[key][1]
                    sys.exit()

        else:
            print("Ok, there are %s sticks on the board.") % (num_sticks)
            print("|")*num_sticks




        #AI player
        guess = random.choice(stick_dict["hat{0}".format(num_sticks)][0])
        if guess > 1:
            print "The computer chose %s sticks" % (guess)
        elif guess == 1:
            print "The computer chose %s stick" % (guess)


        stick_dict["hat{0}".format(num_sticks)].append(guess)
        print stick_dict

        num_sticks = num_sticks - guess


        if num_sticks == 1:
            print("Ok, there is %s stick on the board.") % (num_sticks)
            print("|")*num_sticks

        elif num_sticks < 1:
            print("Player 2 (AI BOT), you lose.")
            #If the AI loses
            for key in stick_dict:
                if len(stick_dict[key]) == 2:
                    del stick_dict[key][1]
            break
        else:
            print("Ok, there are %s sticks on the board.") % (num_sticks)
            print("|")*num_sticks
作为参考,当我运行上面发布的代码时,它在播放器2丢失后停止了迭代,我不得不用键盘中断,而不是让代码循环并继续播放,直到我决定退出

Welcome to the game of sticks! Press <Enter> to continue...
How many sticks are there on the table initially (10-100)?
>>> 10
Ok, there are 10 sticks on the board.
||||||||||
Player 1: How many sticks do you take (1-3)?
>>> 3
Ok, there are 7 sticks on the board.
|||||||
The computer chose 3 sticks
{'hat9': [[1, 2, 3]], 'hat8': [[1, 2, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3]], 'hat2': [[1, 2, 3]], 'hat5': [[1, 2, 3]], 'hat4': [[1, 2, 3]], 'hat7': [[1, 2, 3], 3], 'hat6': [[1, 2, 3]], 'hat10': [[1, 2, 3]]}
Ok, there are 4 sticks on the board.
||||
Player 1: How many sticks do you take (1-3)?
>>> 3
Ok, there is 1 stick on the board.
|
The computer chose 1 stick
{'hat9': [[1, 2, 3]], 'hat8': [[1, 2, 3]], 'hat1': [[1, 2, 3], 1], 'hat3': [[1, 2, 3]], 'hat2': [[1, 2, 3]], 'hat5': [[1, 2, 3]], 'hat4': [[1, 2, 3]], 'hat7': [[1, 2, 3], 3], 'hat6': [[1, 2, 3]], 'hat10': [[1, 2, 3]]}
Player 2 (AI BOT), you lose.

^C---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/andrewthappa/Documents/python/scripts/sticks/human_v_ai.py in <module>()
     29 
     30 
---> 31 while True:
     32         while num_sticks!=0:
     33 

您需要重置计数器

虽然num_sticks不需要在您指向的位置重置,但您认为需要重置它是正确的。当玩家1或人工智能失败时,这就是num_sticks应该被重置的地方。无论如何,谢谢你的指针。出于好奇,我只想运行循环100次。我尝试过递增一个计数器,然后将最外层的while循环设置为while count<100,但当计数器超过100后,循环继续进行。你对怎么做有什么建议吗?
while True:
    num_sticks = 10
    while num_sticks!=0:
        ...