2014-10-17 79 views
-2

我想用Python编写一个Rock,Paper Scissors程序,但是我不断收到这个错误,我不知道如何解决它。岩石剪刀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...') 
+1

determine_winner =(computer_choice,player_choice)应该是 “结果= determine_winner(computer_choice,player_choice)” – 2014-10-17 15:10:33

+4

真的,这个方案是这样充满错误。在发布之前,请先用[pylint](http://www.pylint.org/)等测试它。 – RickyA 2014-10-17 15:20:41

+0

是的,或者得到一个IDE(例如Pycharm社区版)也将帮助指出明显的错误。 – 2014-10-17 15:28:02

回答

0

错误1:

修复line27: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' 

外主(到第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') 

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

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

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