岩纸剪刀Python程序

时间:2014-10-17 15:08:49

标签: python wing-ide

我正在尝试使用Python编写Rock,Paper Scissors程序,但我一直收到此错误,而且我不确定如何修复它。

 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...')

3 个答案:

答案 0 :(得分:0)

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

外部主要(到第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;那里也有错误。

答案 1 :(得分: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()

答案 2 :(得分:0)

您不认为,这段代码会这样缩短:

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!??!?!?")