简体   繁体   English

在某个地方获得无限循环

[英]Getting an endless loop somewhere

I need some help with this program I am trying to finish. 我需要完成此程序的一些帮助。 This thing is kind of a mess. 这东西有点乱。 its the end of a 12 hour day and it's due tonight and I think I'm just making things worse at this point. 这是一天12小时的结束,应该在今晚进行,我想我现在要把事情变得更糟。 I'm struggling with implementing input of "4" for the player_choice and getting the program to just say 'Exiting the program' and stop. 我正在努力为player_choice实现输入“ 4”,并使程序只说“退出程序”并停止。 When I input a choice currently, I get what I think is an endless loop because the entire IDE locks up and crashes. 当我当前输入一个选择时,我会得到一个无限循环,因为整个IDE都将锁定并崩溃。

program requirements: 计划要求:

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.  

Here is the program code: 这是程序代码:

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()

Your program will run infinitely because you are not updating the variable choice2 inside display_menu() function. 您的程序将无限运行,因为您没有更新display_menu()函数中的变量choice2 What you are doing: 你在做什么:

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

So, ultimately choice2 is always 0 and your while loop keeps running endlessly. 因此,最终choice2始终为0,而​​while循环不断运行。

Edit : You are returning a value from your process_player_choice() and assigning the value to a variable player_choice inside display_menu() function. 编辑 :您正在从process_player_choice()返回一个值,并将该值分配给display_menu()函数中的变量player_choice

player_choice = process_player_choice()

I guess you should assign the return value to the variable choice2 so that when user gives 4 as input, program will be terminated. 我猜您应该将返回值分配给变量choice2以便当用户输入4作为输入时,程序将终止。

choice2 = process_player_choice()

Note that , you have another while loop inside determine_winner() which will run endlessly as well because of choice2 . 请注意,choice2 determine_winner()还有另一个while循环,由于choice2 ,该循环也将无限期地运行。 I believe that while loop is not required since you have a while loop already inside display_menu() function. 我相信while循环不是必需的,因为display_menu()函数中已经有一个while循环。

So, in short you actually don't need an additional variable player_choice , you just need one variable that will store the user choice and can be passed to required functions to execute their operations. 因此,简而言之,实际上您不需要一个额外的变量player_choice ,您只需要一个变量即可存储用户的选择,并且可以将其传递给所需的函数来执行其操作。 You should also omit the while loop from the display_menu() function. 您还应该从display_menu()函数中省略while循环。

Lastly, as @AdrianoMartins mentioned in his answer, the following variables that you declared inside display_menu() should be global, otherwise you won't be able to access them from main() function. 最后,就像@AdrianoMartins在他的回答中提到的那样,您在display_menu()内部声明的以下变量应该是全局变量,否则您将无法从main()函数访问它们。

number_of_tied_games = 0
number_of_player_games = 0
number_of_computer_games = 0

You can declare them in global scope and then declare them as global inside display_menu() to modify their value. 您可以在全局范围内声明它们,然后在display_menu()中将它们声明为全局变量以修改其值。 For example: 例如:

// 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

To learn more, i encourage you to see this SO post . 要了解更多信息,我鼓励您阅读这篇SO帖子

The issue is related to the determine_winner method. 该问题与determine_winner方法有关。 The code inside the method will run while the value of choice2 isn't 4, but you don't change it during the loop, therefore it won't stop looping. 该方法中的代码将在choice2的值不为4时运行,但是您不必在循环期间更改它,因此它不会停止循环。

There are several solutions to this issue, the easiest is simply to not use a loop (after all, you should check the result only once every game played). 有多种解决方案,最简单的方法是不使用循环(毕竟,每次玩游戏都只检查一次结果)。

def determine_winner(player_choice, computer_choice, choice2):
    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

That said, there are other errors that you should fix: 也就是说,还有其他错误应修复:

  • The loop location is wrong. 循环位置错误。 You should display the menu and get the data every time the a match ends. 比赛结束后,您应该显示菜单并获取数据。 (@line75) (@ line75)
  • You have added the choice2 variable but you never changed it. 您已经添加了choice2变量,但从未更改过它。 I believe it should be player_choice instead. 我认为应该改为player_choice (@lines 62~74) (@行62〜74)
  • By removing the choice2 variable, you don't need to pass it to the determine_winner method. 通过删除choice2变量,您无需将其传递给choice2方法。
  • Remember that the "number_of_*_games" variables should declared on the global escope and later on global, otherwise you won't be able to access them later on main() method. 请记住,“ number_of _ * _ games”变量应在全局范围内声明,然后在全局范围内声明,否则以后将无法在main()方法上访问它们。

The full complete code is: 完整的完整代码是:

import random

tied_games = 0
player_games = 0
computer_games = 0

def main():
    display_menu()
    print('There were', tied_games, 'tie games played.')
    print('The computer won', computer_games, 'game(s).')
    print('You won', 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):
    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():
    global tied_games
    global player_games
    global computer_games

    player_choice = 0

    while player_choice != "4":
        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()

        if player_choice != "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)
            if result == 'computer':
                computer_games += 1
            elif result == 'player':
                player_games += 1
            else:
                tied_games += 1


if __name__ == '__main__':
    main()

I believe that's all :) 我相信就这样:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM