Python 3:Rock,Paper,Scissor游戏 - 如何定义胜出的计数

时间:2018-03-21 22:17:08

标签: python-3.x count counter user-defined-functions

我是Python的新手。需要在游戏结束时打印出Python 3中的Rock,Paper,Scissors游戏:

The number of rounds the computer has won.
The number of rounds the user has won.
The number of rounds that ended in a tie
The number of times the user selected each weapon (Rock, Paper, Scissors).

注意:计算机应该根据用户之前选择的武器选择最有可能击败用户的武器,但是它的选择将在以后显示,因此用户不会看到它。对于该计算机,计算用户选择每种武器(岩石,纸张或剪刀)的次数。

例如,如果用户选择了Rock and Paper各3次,Scissors只选择了1次,或者如果用户选择了相同次数的每种武器,那么就没有最常用的武器由用户;在这种情况下,计算机可以选择任何武器。

我的代码是下一个:

# Import random function
import random

# After the round ask the user if he/she would like to play the game again
def display_intro():
    print("Welcome to Rock, Paper and Scissors Game!")
    play_game = input("Would you like to play with me?\n"
                  "y/n").lower()
    if play_game == "y" or play_game == "yes" or play_game == "Y":
        print("Let\'s start playing!")
        get_comp_choice()
    elif play_game == "n" or play_game == "no" or play_game == "N":
        print("Thanks for playing")
        print("Computer won", compWinCt, "times.")"" \
        print("User won", userWinCt, "times.")
        print("Draw games count is: ",drawCt)
        print("User picked Rock", userRockCt, "times.")
        print("User picked Paper", userPaperCt, "times.")
        print("User picked Scissors", userScissors, "times.")
        quit()
    else:
        print("That's now a valid choice. Please try again!")
    display_intro()


# define random choice pick
def get_random_choice():
    comp_choice = random.randint(1, 3)
    if comp_choice == 1:
        comp_choice = "r"
    elif comp_choice == 2:
        comp_choice = "p"
    elif comp_choice == 3:
        comp_choice = "s"
    return comp_choice

# define computer choice base on what user has chosen in previous rounds
def get_comp_choice():
    if userRockCt == 0 and userPaperCt == 0 and userScissorsCt == 0:
        return get_random_choice()
    else:
        if (userRockCt > userPaperCt) and (userRockCt > userScissorsCt):
           return comp_choice == "r"
        elif (userPaperCt > userRockCt) and (userPaperCt > userScissorsCt):
            return comp_choice == "p"
        elif (userScissorsCt > userPaperCt) and (userScissorsCt > userRockCt):
            return comp_choice == "s"

def main():
while True:
    # set count for a zero
    userRockCt = 0
    userPaperCt = 0
    userScissorsCt = 0
    compWinCt = 0
    userWinCt = 0
    drawCt = 0
    # Get computer choice
    comp_choice = get_comp_choice()
    # Get user choice from the input
    user_choice = input("Please enter from the menue: \n"
                        "r for rock,"
                        "p for paper"
                        "s for scissors"
                        "q to quit")

    # Check who has won /lost or if the game is draw
    # Get the count for how many times user picke Rock, Paper or Scissors
    # Get the count for how many times won computer and user and the count for the draw
    while comp_choice == "r":
        if user_choice == "r":
            print("This is a DRAW. You chose Rock and Computer chose Rock.")
            userRockCt += 1
            drawCt += 1
        elif user_choice == "p":
            print("YOU WIN. You chose Paper and Computer chose Rock. Paper covers Rock.")
            userPaperCt += 1
            userWinCt += 1
        elif user_choice == "s":
            print("YOU LOSE. You chose Scissors and Computer chose Rock. Rock breaks Scissors.")
            userScissorsCt += 1
            compWinCt += 1
        else:
            print("Try again")
        display_intro()

    while comp_choice == "p":
        if user_choice == "r":
            print("YOU LOSE. You chose Rock and Computer chose Paper. Paper covers Rock.")
            userRockCt += 1
            compWinCt += 1
        elif user_choice == "p":
            print("This is a DRAW. You chose Rock and Computer chose Rock.")
            userPaperCt += 1
            drawCt += 1
        elif user_choice == "s":
            print("YOU WIN. You chose Scissors and Computer chose Paper. Scissors cut Paper.")
            userScissorsCt += 1
            userWinCt += 1
        display_intro()

    while comp_choice == "s":
        if user_choice == "r":
            print("YOU WIN. You chose Rock and Computer chose Scissors. Rock breaks Scissors.")
            userRockCt += 1
            userWinCt += 1
        elif user_choice == "p":
            print("YOU LOSE. You chose Paper and Computer chose Scissors. Scissors cut Paper.")
            userPaperCt += 1
            compWinCt += 1
        elif user_choice == "s":
            print("This is a DRAW. You chose Scissors and Computer chose Scissors.")
            userScissorsCt += 1
            drawCt += 1
        display_intro()


main()

我不知道我是否错误地定义了定义函数的顺序或丢失了某些东西。当我运行代码时,我得到了这个错误。

Traceback (most recent call last):
  File "C:/Users/.../Test", line 123, in <module>
    main()
  File "C:/Users/.../Test", line 64, in main
    comp_choice = get_comp_choice()
  File "C:/Users/.../Test", line 43, in get_comp_choice
    if userRockCt == 0 and userPaperCt == 0 and userScissorsCt == 0:
NameError: name 'userRockCt' is not defined

Process finished with exit code 1

只是查看代码,我可以告诉同样的问题是userPaperCt,userScissorsCt在get_comp_choice

和def display_intro()with compWinCt,userWinCt和DrawCt

我见过一些使用全局的例子,但我试图避免使用它。

1 个答案:

答案 0 :(得分:0)

将'userRockCt'声明为全局变量将解决此问题。

# Import random function
import random

# Declaring the variable here
userRockCt = 0

# Rest of the code goes here

def main():
while True:
   # Declaring the variable as global here
   # set count for a zero
   global userRockCt

   # rest of your code

main()