user3424161
user3424161

Reputation: 13

Rock Paper Scissors function help in Python

I just started learning Python, and I am trying to write a basic Rock Paper Scissors program for my assignment. The game is intended to go on for 10 rounds, while keeping track the score between the player and the computer. I have two specific problems with it.

import random

def welcome_prompt():
    print ("ROCKER PAPER SCISSORS in PYTHON Assignment")        
    print ("Rules: Rocks beats Scissors, Scissors beats Paper, Paper beats Rock")

def get_player_move():
    print ('Round ' + str(round))        
    print ("Please play one of the following")
    get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

    if get_player_move == ("R"):
        print ("You used Rock!") 
        return 1              

    elif get_player_move == ("P"):        
        print ("You used Paper!")
        return 2

    elif get_player_move == ("S"):
        print ("You used Scissors!")
        return 3

    else:
        print "Invalid input, please use capitalized initial (R,P,S)"
        return get_player_move()

def get_computer_move():
        get_computer_move = random.randint(1,3)

        if get_computer_move == 1:
            print ("Computer used Rock!")
        return 1

    elif get_computer_move == 2:
        print ("Computer used Paper!")
        return 2

    elif get_computer_move == 3:
        print ("Computer used Scissors!")
        return 3



def compare_moves(get_player_move, get_computer_move):
# Rock = 1
# Paper = 2
# Scissors = 3

    if (get_player_move ==  1 and get_computer_move == 1) or (get_player_move == 2 and get_computer_move == 2) or (get_player_move == 3 and get_computer_move == 3):
    print ("It's a tie!")
    return 0  

    elif (get_player_move == 1 and get_computer_move == 3) or (get_player_move == 2 and get_computer_move == 1) or (get_player_move == 3 and get_computer_move == 2):
    print ("You win the round!")
    return 1

    elif (get_player_move == 1 and get_computer_move == 2) or (get_player_move == 2 and get_computer_move == 3) or (get_player_move == 3 and get_computer_move == 1):
    print ("You lose the round!")
    return -1

    elif (get_player_move == 4):
    print ("You didn't put in correct input, computer gets a free win")
    return -1


# Game Program

player_score = 0
comp_score = 0
round = 0


welcome_prompt()

('Round ' + str(round))
while round< 10:
    round = round + 1
    get_player_move()
    get_computer_move()
    compare_moves(get_player_move, get_computer_move)

    if compare_moves == 1:
        player_score = player_score + 1
        print 'Player Score'+ str(player_score)
        print 'Computer Score'+ str(player_score)
    elif compare_moves == -1:
        comp_score = comp_score + 1
        print 'Player Score'+ str(player_score)
        print 'Computer Score'+ str(player_score)        

print "Game Over"

Firstly, I can't get the compare_move function to recall the returned values from both get_player_move and get_computer_move. The game can run without any error, but it just skips the comparison/ score component completely. I am still a bit iffy with the basics, so not exactly sure what is missing.

Secondly, in the get_player_move function, when I enter an invalid input (example: blah) to test the raw_input, it gives an error.

Traceback (most recent call last):
  File "C:\Python27\Rock Paper Scissors.py", line 85, in <module>
    get_player_move()
  File "C:\Python27\Rock Paper Scissors.py", line 32, in get_player_move
    return get_player_move()
TypeError: 'str' object is not callable

So how do you make a function to asks for the correct raw_input again after entering invalid input, without interrupting the while loop?

Explanation is greatly appreciated, thank you

Upvotes: 1

Views: 12467

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122132

You have a local variable get_player_move inside the function get_player_move(); you cannot then still use the function name (a global).

Rename the get_player_move local variable.

So, instead of:

get_player_move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

use:

move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")

perhaps.

To get user input, it's best not to rely on recursion, however. The user could hit 'C' forever and then your program would crash with an RuntimeError: maximum recursion depth exceeded. It's easier to use a loop instead:

while True:
    move = raw_input(" 1) [R]ock, 2) [P]aper, 3) [S]cissors:")
    if move == "R":
        print ("You used Rock!") 
        return 1              

    # etc.

    else:
        print "Invalid input, please use capitalized initial (R,P,S)"

Because you return from the function when a correct choice is made, the loop automatically is exited as well. If however you get to the end and Invalid input is printed, the while True loop starts at the top again and the user is asked once more to enter a choice.

Next: although your function returns a choice (an integer), you never store that return value. You must store it where you called the function:

player_move = get_player_move()
computer_move = get_computer_move()
result = compare_moves(player_move, computer_move)

if result == 1:

Note that it's not the function name that holds the return value; it's a separate variable. player_move is assigned whatever the get_player_move() returned, for example.

You can then pass these returned values to compare_moves(); it also returns a result, here stored in result for further comparisons.

Upvotes: 2

Related Questions