python - 在某处陷入无限循环

标签 python

我需要一些帮助来完成我正在尝试完成的这个程序。这件事有点乱。这是一天 12 小时的结束,它将于今晚到期,我认为此时我只是让事情变得更糟。我正在努力为 player_choice 实现输入“4”并让程序只说“退出程序”并停止。当前,当我输入一个选项时,我得到了我认为是无限循环的结果,因为整个 IDE 锁定并崩溃了。

项目要求:

1) Get and return the value of the computer’s choice as an integer.  
2) Use a menu to get, validate, and return the player’s choices.  
The player will enter their menu choice for rock, paper, scissors, or quit.  
Validate the choice before returning it.
3) Determine the winner.  There are a total of 9 possible combinations of
computer and player choices.  The menu should always be displayed after the outcome has 
been displayed regardless of the outcome 
(i.e. ties should be treated the same as any other outcome). 
4) After the player chooses to stop playing, display the total number of
 games that ended in a tie, the total number the computer won, and the total
 number the player won.  

程序代码如下:

import random

def main():
    display_menu()   
    print('There were', number_of_tied_games, 'tie games played.')
    print('The computer won', number_of_computer_games, 'game(s).')
    print('You won', number_of_player_games, 'game(s).')

def process_computer_choice():
    choice1 = random.randint(1,3)
    return choice1

def process_player_choice():
    print('What is your choice?')
    choice2 = (input())
    while choice2 != "1" and choice2 != "2" and choice2 != "3" and choice2 != "4":
        print("ERROR: the choice can only be 1, 2, 3, or 4.")
        choice2 = (input("Please enter a correct choice: ")) 
    return choice2

def determine_winner(player_choice, computer_choice, choice2):
    while choice2 != "4":
        if computer_choice == 1:
            if player_choice == "2":
                print('Paper covers rock. You win!')
                winner = 'player'
            elif player_choice == "3":
                print("Rock crushes scissors. The computer wins!")
                winner = 'computer'
            else:
                print('The game is tied. Try again.')
                winner = 'tied'
        if computer_choice == 2:
            if player_choice == "1":
                print('Paper covers rock. The computer wins!')
                winner = 'computer'
            elif player_choice == "3":
                print("Scissors cuts paper. You win!")
                winner = 'player'
            else:
                print('The game is tied. Try again.')
                winner = 'tied'
        if computer_choice == 3:
            if player_choice == "1":
                print('Rock smashes scissors. You win!')
                winner = 'player'
            elif player_choice == "2":
                print("Scissors cuts paper. The computer wins!")
                winner = 'computer'
            else:
                print('The game is tied. Try again.')
                winner = 'tied'
    return winner

def display_menu(): 
    choice2 = 0
    number_of_tied_games = 0
    number_of_player_games = 0
    number_of_computer_games = 0   

    print('        MENU')
    print('1) Rock')
    print('2) Paper')
    print('3) Scissors')
    print('4) Quit')
    print("Let's play the game of Rock, Paper, Scissors.")
    computer_choice = process_computer_choice()
    player_choice = process_player_choice()    
    while choice2 != "4":

        if computer_choice == 1:
            print('The computer chooses rock.')
        elif computer_choice == 2:
            print('The computer chooses paper.')
        else:
            print('The computer chooses scissors.')
        #display player choice
        if player_choice == "1":
            print('You choose rock.')
        elif player_choice == "2":
            print('You choose paper.')
        else:
            print('You choose scissors.')

        result = determine_winner(player_choice, computer_choice, choice2)
        if result == 'computer':
            number_of_computer_games += 1
        elif result == 'player':
            number_of_player_games += 1
        else:
            number_of_tied_games += 1
        print    


main()

最佳答案

您的程序将无限运行,因为您没有更新 display_menu() 函数内的变量 choice2。你在做什么:

choice2 = 0
while choice2 != "4":
     # your code
     result = determine_winner(player_choice, computer_choice, choice2)

因此,最终 choice2 始终为 0,并且您的 while 循环一直在无休止地运行。

编辑:您正在从 process_player_choice() 返回一个值,并将该值分配给 display_menu( ) 函数。

player_choice = process_player_choice()

我猜你应该将返回值赋给变量choice2,这样当用户输入4时,程序就会终止。

choice2 = process_player_choice()

请注意,您在 determine_winner() 中有另一个 while 循环,由于 choice2,它也会无限运行。我相信不需要 while 循环,因为 display_menu() 函数中已经有一个 while 循环。

所以,简而言之,您实际上不需要额外的变量 player_choice,您只需要一个变量来存储用户选择并可以传递给所需的函数来执行它们的操作。您还应该从 display_menu() 函数中省略 while 循环。

最后,正如@AdrianoMartins 在他的回答中提到的,您在 display_menu() 中声明的以下变量应该是全局的,否则您将无法从 main( ) 函数。

number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0

您可以在全局范围内声明它们,然后在 display_menu() 中将它们声明为全局以修改它们的值。例如:

// declaring globally
tied_games = 0
player_games = 0
computer_games = 0

def display_menu():
    // need to modify global copy of the variables
    global tied_games
    global player_games
    global computer_games

要了解更多信息,我鼓励您查看此 SO post .

关于python - 在某处陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41133010/

相关文章:

python - 如何获取函数的关键字参数

python - 使用 xpath、lxml、python 的父属性条件的元素路径

python - 图例的顺序

python - 沿坐标列表给定的路径矢量化 haversine 距离计算

python - pandas 的assign() 函数和apply() 函数有什么区别?

python - 如何使用 Python 对 Spark 中的 LIBSVM 文件进行特征选择和缩减?

python - 使用 AWS Gateway API 代理从 Python AWS Lambda 重定向

python - 生产中未调用错误处理程序

python - 如何在django请求中获取POST、DELETE

python - OSX 和 Ubuntu 之间俄语语言包的差异,如何为 strftime 获得相同的输出?