【问题标题】:Rock Paper Scissors Python Program石头剪刀布Python程序
【发布时间】:2014-10-17 15:08:49
【问题描述】:

我正在尝试使用 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

这是我的程序:

import random

def main():
    win = 0
    lose = 0
    tie = 0

    ROCK_CHOICE = '1'
    PAPER_CHOICE = '2'
    SCISSORS_CHOICE = '3'
    QUIT_CHOICE = '4'   

    play_again = 'y'        
    while play_again == 'y':            
        print('Welcome to the game of paper, rock, scissors!')
        print('Please input the correct number according')
        print('to the choices given.')

        computer_choice = get_computer_choice()
        player_choice = get_player_choice()
        determine_winner = (computer_choice, player_choice)

        print('Computer choose', computer_choice, '.')
        print('You choose', player_choice, '.')

        determine_winner(computer_choice, player_choice)
        if result == -1:
            lose += 1
        elif result == 0:
            tie += 1
        else:
            win += 1

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

def get_computer_choice():
    choice = random.randint(1,4)        
    if choice == 1:
        choice = 'ROCK'
    elif choice == 2:
        choice = 'PAPER'
    elif choice == 3:
        choice = 'SCISSORS'
    else:
        choice = 4                
    return choice

def get_player_choice():
    choice = int(input('Select rock(1), paper(2), or scissors(3): '))        
    while choice != 1 and choice != 2 and choice != 3:
        print('The valid numbers are rock choice 1), paper choice 2),')
        print('or scissors choice 3).')
        choice = int(input('Please a valid number: '))

    if choice == 1:
        choice = 'ROCK'
    elif choice == 2:
        choice = 'PAPER'
    elif choice == 3:
        choice = 'SCISSORS'
    else:
        choice = 4
    return choice

def determine_winner(computer_choice, player_choice):
    if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
        print('Its a tie.')
        return 0
    elif player_choice == PAPER_CHOICE and computer_choice == PAPER_CHOICE:
        print('Its a tie.')
        return 0
    elif player_choice == SCISSORS_CHOICE and computer_choice == SCISSORS_CHOICE:
        print('Its a tie.')
        return 0      
    elif player_choice == ROCK_CHOICE and computer_choice == PAPER_CHOICE:
        print('You lose, Rock covers Paper.')
        return -1       
    elif player_choice == ROCK_CHOICE and computer_choice == SCISSORS_CHOICE:
        print('You WIN!!! Rock smashes Scissors.')
        return 1       
    elif player_choice == PAPER_CHOICE and computer_choice == SCISSORS_CHOICE:
        print('You lose, Scissors cuts Paper.')
        return -1
    elif player_choice == SCISSORS_CHOICE and computer_choice == ROCK_CHOICE:
        print('You lose, Rock smashes Paper.')
        return -1       
    elif player_choice == SCISSORS_CHOICE and computer_choice == PAPER_CHOICE:
        print('You WIN!!! Scissors cuts Paper.')
        return 1       
    elif player_choice == PAPER_CHOICE and computer_choice == ROCK_CHOICE:
        print('You WIN!!! Paper covers Rock.')
        return 1       
    else:
        player_choice == QUIT_CHOICE
        print('Exiting the program...')  
        return

def display_results():
    print()
    print('         MENU')
    print('1) Rock')
    print('2) Paper')
    print('3) Scissors')
    print('4) Quit')

    player_choice = input('Enter your choice:')
    while player_choice != '1' and player_choice != '2' and \
          player_choice != '3' and player_choice != '4':
        print()
        print('Error: invalid selection.')
        player_choice = input('Please re-enter your choice:')

    print('Your total wins are', win, '.')
    print('Your total losses are', lose, '.')
    print('Your total ties are', tie, '.')        

main()
input('\nPress ENTER to continue...')

【问题讨论】:

  • determine_winner = (computer_choice, player_choice) 应该是“result = determine_winner(computer_choice, player_choice)”
  • 真的,这个程序充满了错误。请在发布之前先使用pylint 之类的内容进行测试。
  • 是的,或者获取一个 IDE(例如 Pycharm 社区版)也将有助于指出明显的错误。

标签: python wing-ide


【解决方案1】:

错误 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

移动:

ROCK_CHOICE = '1'
PAPER_CHOICE = '2'
SCISSORS_CHOICE = '3'
QUIT_CHOICE = '4'

main 外(到第 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

输入应该是 raw_input 第 41 行:

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

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

NNBB。如果您打算使用display_results;里面也有错误。

【讨论】:

    【解决方案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()
    

    【讨论】:

      【解决方案3】:

      你不觉得,这样代码会更短:

      print("R for rock")
      print("P for paper")
      print("S for scissors")
      choices = ["rock", "paper", "scissor"]
      
      
      while True:
          choice = random.choice(choices)
          player_choice = input("choose> ")
          if player_choice.lower() == "r":
              if choice == "rock":
                  print("i picked rock too!")
              elif choice == 'paper':
                  print("YOU LOST! i picked paper")
              else:
                  print("fine, you win")
          elif player_choice.lower() == "p":
              if choice == "rock":
                  print("You win.... Whatever!")
              elif choice == 'paper':
                  print("haha same decisions")
              else:
                  print("I won! You should be ashamed of losing against a bot")
          elif player_choice.lower() == "s":
              if choice == "rock":
                  print("I won! Humanity will soon be controlled by bots")
              elif choice == 'paper':
                  print("Alright, you win")
              else:
                  print("Draw")
          else:
              print("YOU WANNA PLAY GAMES WITH ME!??!?!?")
      

      【讨论】:

        猜你喜欢
        • 2017-02-10
        • 2018-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 1970-01-01
        相关资源
        最近更新 更多