石头-布-剪刀-Python程序

2024-09-27 07:34:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python编写一个石头剪刀程序,但是我一直遇到这个错误,我不知道如何修复它。在

 File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 140, in <module>
  File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 34, in main
builtins.TypeError: 'tuple' object is not callable

这是我的节目:

^{pr2}$

Tags: inpydebug程序srclineidefile
2条回答
from random import randint

class RPS:
    def __init__(self, pCh):
        self.playChoice = pCh
        self.compChoice = ""
        self.choice = randint(0, 3)
        self.winner = ""
        self.uCompChoice = ""
        self.uPlayChoice = ""
        if self.choice == 0:
            self.compChoice = "R"
        elif self.choice == 1:
            self.compChoice = "P"
        else:
            self.compChoice = "S"

    if self.compChoice == "R":
        self.uCompChoice = "Rock"
    if self.compChoice == "P":
        self.uCompChoice = "Paper"
    if self.compChoice == "S":
        self.uCompChoice = "Scissors"
    if self.playChoice == "P" or self.playChoice == "p" or self.playChoice == "Paper" or self.playChoice == "paper" or self.playChoice == "PAPER" or self.playChoice == "2":
        self.uPlayChoice = "Paper"
    if self.playChoice == "S" or self.playChoice == "s" or self.playChoice == "Scissors" or self.playChoice == "scissors" or self.playChoice == "SCISSORS" or self.playChoice == "3":
        self.uPlayChoice = "Scissors"
    if self.playChoice == "R" or self.playChoice == "r"  or self.playChoice == "Rock"  or self.playChoice == "rock"  or self.playChoice == "ROCK" or self.playChoice == "1":
        self.uPlayChoice = "Rock"

def determineWinner(self):
    if self.uCompChoice == self.uPlayChoice:
        return "Draw Game!"
    elif self.uCompChoice == "Rock":
        if self.uPlayChoice == "Paper":
            return "You won because Paper covers Rock!!"
        else:
            return "Computer wins because Rock breaks Scissors."
    elif self.uCompChoice == "Paper":
        if self.uPlayChoice == "Scissors":
            return "You won because Rock cuts Paper!!"
        else:
            return "Computer wins because Paper covers Rock."
    else:
        if self.uPlayChoice == "Rock":
            return "You won because Rock breaks Scissors!!"
        else:
            return "Computer wins because Scissors cuts Paper."



def __str__(self):
    return "Your weapon: " + self.uPlayChoice + "\n" + "Computer's weapon " + self.uCompChoice + "\n" + \
           self.determineWinner()

def main():
    print("Welcome to RockPaperScissors.")
    print("Pick your weapon: ")
    print("1. Rock")
    print("2. Paper")
    print("3. Scissors")
    print("Note: If you don't pick a valid weapon the computer will pick a random weapon for you")
    # input from user (weapon)
    weapon = input("")
    # Creating a new object
    attack = RPS(weapon)
    # printing the result
    print(attack.__str__())
    # Asking the user if he wants to play again
    playagain = input("Would you like to play again: \n")
    if playagain == "Yes" or playagain == "yes" or playagain == "YES" or playagain == "y" or playagain == "Y":
        main()
    else:
        print("Thank you for playing.")


main()

错误1:

修复第27行:result = determine_winner(computer_choice, player_choice

错误2:

if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
NameError: global name 'ROCK_CHOICE' is not defined

移动:

^{pr2}$

外干管(至3号线)

错误3:

File "main.py", line 41, in main
    play_again = input('Play again? Enter y for yes')
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined

输入应为原始输入行41:

play_again = raw_input('Play again? Enter y for yes')

注意。而且input('\nPress ENTER to continue...')也需要是raw_input

NNBB公司。如果您计划使用display_results;其中也有错误。在

相关问题 更多 >

    热门问题