Global name error in modified RPS code


#1

I wanted to make the RPS.py code a little more functional and allow the player to keep playing so I modified the code a bit. However, something strange happens where the RPS.py will run n times before throwing a global name error. Here’s a recent snippet from the python shell after watching this happen:

================== RESTART: D:/Python27/Scripts/play_RPS.py ==================
Would you like to play Rock, Paper, Scissors? Yes
Enter Rock, Paper, or Scissors: Rock
User choice: ROCK
Computer choice: PAPER
Aww, you lost
Would you like to play Rock, Paper, Scissors? Yes
Enter Rock, Paper, or Scissors: Paper
User choice: PAPER
Computer choice: PAPER
Yawn, it’s a tie
Would you like to play Rock, Paper, Scissors? Yes
Enter Rock, Paper, or Scissors: Scissors
User choice: SCISSORS
Computer choice: SCISSORS
Yawn, it’s a tie
Would you like to play Rock, Paper, Scissors? Yes
Enter Rock, Paper, or Scissors: Rock
User choice: ROCK
Computer choice: ROCK
Yawn, it’s a tie
Would you like to play Rock, Paper, Scissors? yes
Enter Rock, Paper, or Scissors: Rock
User choice: ROCK
Computer choice: PAPER
Aww, you lost
Would you like to play Rock, Paper, Scissors? yes
Enter Rock, Paper, or Scissors: Scissors
User choice: SCISSORS
Computer choice: SCISSORS
Yawn, it’s a tie
Would you like to play Rock, Paper, Scissors? yes
Enter Rock, Paper, or Scissors: Scissors
User choice: SCISSORS
Computer choice: ROCK

Traceback (most recent call last):
File “D:/Python27/Scripts/play_RPS.py”, line 41, in
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 37, in play_RPS
ask_play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 14, in ask_play_RPS
play_RPS()
File “D:/Python27/Scripts/play_RPS.py”, line 36, in play_RPS
decide_winner(user_choice, computer_choice)
File “D:/Python27/Scripts/play_RPS.py”, line 27, in decide_winner
elif user_choice == options[2] and computer_choice == option[1]:
NameError: global name ‘option’ is not defined

I expected the program to run smoothly until I entered some form of ‘no’ into the console when asked if I wanted to play.

Here’s my code:

"""
This program will simulate the game 'Rock, Paper, Scissors' with the player against the computer.
"""

import random

options = ["ROCK", "PAPER", "SCISSORS"]
message = {"tie": "Yawn, it's a tie", "won" : "Yay, you won!", "lost" : "Aww, you lost"}

def ask_play_RPS():
    answer = raw_input("Would you like to play Rock, Paper, Scissors? ")
    answer = answer.upper()
    if answer == "YES":
        play_RPS()
    else:
        exit

def decide_winner(user_choice, computer_choice):
    print "User choice: %s" % (user_choice)
    print "Computer choice: %s" % (computer_choice)
    if user_choice == computer_choice:
        print message["tie"]
    elif user_choice == options[0] and computer_choice == options[2]:
        print message["win"]
    elif user_choice == options[1] and computer_choce == options[0]:
        print message["win"]
    elif user_choice == options[2] and computer_choice == option[1]:
        print message ["win"]
    else:
        print message["lost"]
  
def play_RPS():
    user_choice = raw_input("Enter Rock, Paper, or Scissors: ")
    user_choice = user_choice.upper()
    computer_choice = options[random.randint(0,2)]
    decide_winner(user_choice, computer_choice)
    ask_play_RPS()

ask_play_RPS()

Thanks in advanced!


#2

Is the white space intentional?

Another approach would be to run the code until the user wants out. In other words, no “Do you want to play?” question, only an exit condition at the user’s disposal.

Jump straight into the game and exit when the user inputs an empty string.

>>> type(raw_input('_'))
_
<type 'str'>
>>> len(raw_input('_'))
_
0
>>> 

In both instances all I did was press Enter.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.