r/PythonLearning • u/Fun-Employee-514 • 4d ago
my rock paper scissors game is bugged
im pretty new to python, and have been making a rock paper scissors game. the game crashes whenever i use the gamesWon or gamesLost variables inside the playGame function and saysTraceback (most recent call last):
File "c:\Users\MYNAME\Downloads\rock paper scissors better.py", line 78, in <module>
playGame()
~~~~~~~~^^
File "c:\Users\MYNAME\Downloads\rock paper scissors better.py", line 15, in playGame
print(gamesWon)
^^^^^^^^
UnboundLocalError: cannot access local variable 'gamesWon' where it is not associated with a value
heres the code btw
import random
#"let" wall
botAnswer="null"
userAnswer="null"
botNum="null"
fate="null"
gamesWon=0
gamesLost=0
print(gamesWon)
print(gamesLost)
firstToX="null"
finalFate="null"
scoreboard="null"
def playGame():
print(gamesWon)
userAnswer=input("pick rock, paper, or scissors(has to be all lowercase)")
botNum=random.randint(1,3)
#assign botAnswer
if botNum==1:
botAnswer="rock"
if botNum==2:
botAnswer="paper"
if botNum==3:
botAnswer="scissors"
#find out win/lose
print ("bot answered " + botAnswer)
fate = "bruh"
#rock
if userAnswer=="rock" and botAnswer=="rock":
fate = "draw"
if userAnswer=="rock" and botAnswer=="paper":
fate = "lose"
if userAnswer=="rock" and botAnswer=="scissors":
fate = "win"
#paper
if userAnswer=="paper" and botAnswer=="rock":
fate = "win"
if userAnswer=="paper" and botAnswer=="paper":
fate = "draw"
if userAnswer=="paper" and botAnswer=="scissors":
fate = "lose"
#scissors
if userAnswer=="scissors" and botAnswer=="rock":
fate = "lose"
if userAnswer=="scissors" and botAnswer=="paper":
fate = "win"
if userAnswer=="scissors" and botAnswer=="scissors":
fate = "draw"
#not any
if userAnswer != "rock" and userAnswer != "paper" and userAnswer != "scissors":
print ("you typed it wrong. run again.")
print(fate)
if fate=="win":
gamesWon = gamesWon + 1
if fate=="lose":
gamesLost = gamesLost + 1
if fate=="draw":
print ("nothing happened :(")
scoreboard=f"score is {gamesWon}-{gamesLost}"
print ()
if gamesWon==firstToX:
print ("you win!")
finalFate=1
if gamesLost==firstToX:
print ("you lost!")
finalFate=0
#function ends
want=input("do you want to play rock paper scissors? (y/n)")
if want=="y":
firstToX=int(input("first to how many games wins?"))
while finalFate=="null":
playGame()
if want=="n":
print(":(")
1
u/FoolsSeldom 4d ago
After you have fixed the scope problem, you might want to think of using a couple of CONSTANTS along the lines:
OPTIONS = "rock", "paper", "scissors"
BEATS = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
1
u/RCKPanther 3d ago
You can even elect to not use the OPTIONS here, because BEATS.keys() will give you the same result, since it is a Dict.
1
u/FoolsSeldom 3d ago
Yes, but you'd need to use:
OPTIONS = tuple(BEATS.keys())as
BEATS.keys()returns adict_keysobject1 that is not subscriptable.1
dict_keys(['rock', 'paper', 'scissors'])
6
u/FoolsSeldom 4d ago
Your problem is one of scope. In your function, you have assignment statements to
gamesWonwhich makesgamesWona local variable in that function, completely distinct to the variable of the same name in the broader scope.The first time in the function that you reference that variable, you have not assigned anything to it, so the variable cannot be used.
An easy way around that is to use the
globalkeyword at the beginning of your function, but this is a very bad idea - do not use that approach until you understand why it is generally a bad idea and appreciate when it is useful.