hey yall! complete newbie on coding here. making my first project on python which is a rock paper scissors game and I'm having trouble implementing the score counter system.
When I try to give the variable "user_score" a point if they win, I run into this problem which Pycharm reads: "Unresolved reference 'user_score'. I'd be happy if someone could help me, it's probably just something simple but I'm so new to this lol.
#trouble implementing the score counter
1 messages · Page 1 of 1 (latest)
Functions are something you make for themselves, like you’re making a if-statement, but in your own way. So get the functions out.
If-statement applies once. So you could consider using a while-statement.
When making functions, try to avoid nesting them. Use them after when you’re done with making them.
have gotten the def out of the if
I'm getting stuck at the 19th line of code because I'm trying to assign a new value to the variable but python reads that as an unresolved reference.
Is there any other way we can change the global variable inside a function without putting it in one?
U can use user_score += 1 if it works
I tried to. The scoreboard does turn 0-1 but it gets reset back to 0-0 on the replay
You mind posting the code?
import random
def playgame():
user_input = input("please choose: rock, paper, or scissors? ").lower()
outcomes = ["rock", "paper", "scissors"]
computer_input = random.choice(outcomes)
def showresult(first_hand, second_hand):
print("Rock... Paper... Scissors You chose " + first_hand + " and I chose " + second_hand + "!")
if user_input == "rock" and computer_input == "scissors":
showresult("rock", "scissors")
print("you win!")
elif user_input == "rock" and computer_input == "paper":
showresult("rock", "paper")
print("you lose!")
elif user_input == "paper" and computer_input == "scissors":
showresult("paper", "scissors")
print("you win!")
elif user_input == "paper" and computer_input == "rock":
showresult("paper", "rock")
print("you lose!")
elif user_input == "scissors" and computer_input == "paper":
showresult("scissors", "paper")
print("you win!")
elif user_input == "scissors" and computer_input == "rock":
showresult("scissors", "rock")
print("you lose!")
elif user_input == computer_input:
print("You and I both chose" + user_input)
print("that's a draw")
else:
playgame()
replay = input("would you like to play again? ").lower()
if replay == "yes":
playgame()
else:
print("GOODBYE")
playgame()
Again, you nested your showresult in playgame.
When i put showresult outside the variables become undefined
Do i have to call the variables first hand and second hand outside both functions first?
import random
def playgame():
user_input = input("please choose: rock, paper, or scissors? ").lower()
outcomes = ["rock", "paper", "scissors"]
computer_input = random.choice(outcomes)
showresult(user_input,computer_input)
def showresult(first_hand, second_hand):
print("Rock... Paper... Scissors You chose " + first_hand + " and I chose " + second_hand + "!")
if user_input == "rock" and computer_input == "scissors":
showresult("rock", "scissors")
print("you win!")
elif user_input == "rock" and computer_input == "paper":
showresult("rock", "paper")
print("you lose!")
elif user_input == "paper" and computer_input == "scissors":
showresult("paper", "scissors")
print("you win!")
elif user_input == "paper" and computer_input == "rock":
showresult("paper", "rock")
print("you lose!")
elif user_input == "scissors" and computer_input == "paper":
showresult("scissors", "paper")
print("you win!")
elif user_input == "scissors" and computer_input == "rock":
showresult("scissors", "rock")
print("you lose!")
elif user_input == computer_input:
print("You and I both chose" + user_input)
print("that's a draw")
else:
playgame()
replay = input("would you like to play again? ").lower()
if replay == "yes":
playgame()
else:
print("GOODBYE")
playgame()
I added a showresult in the playgame function. Take a close to your code. In the showresult function, you are trying to use global value, but the user and computer inputs are local.
Wait, hold on. May you explain what you are trying to do/approach?
I didn't wanna rewrite that long line of code under every if statement so I put it as a showresult function
I think I understood that you're trying to make a loop.
Oh yes that last bit is me trying to loop it
Try to avoid using this type of loop. Instead,try while.
import random
def playgame():
user = input("\nplease choose: rock, paper, or scissors? ").lower()
outcomes = ["rock", "paper", "scissors"]
computer = random.choice(outcomes)
return [user,computer]
def showresult(user_input,computer_input):
print("Rock... Paper... Scissors, you chose " + user_input + " and I chose " + computer_input + "!")
if user_input == "rock" and computer_input == "scissors":
print("you win!")
elif user_input == "rock" and computer_input == "paper":
print("you lose!")
elif user_input == "paper" and computer_input == "scissors":
print("you win!")
elif user_input == "paper" and computer_input == "rock":
print("you lose!")
elif user_input == "scissors" and computer_input == "paper":
print("you win!")
elif user_input == "scissors" and computer_input == "rock":
print("you lose!")
elif user_input == computer_input:
print("You and I both chose" + user_input)
print("that's a draw")
while True:
play = input("\nDo you want to play (yes/no)? ")
if play == "yes":
first_hand,second_hand = playgame()
showresult(first_hand,second_hand)
else:
print("GOODBYE")
break
I changed some of your code. It should work now.
Now you could try to add the score system. Remember have the score as global values.
So I should have the "user_score =+ 1" also outside functions
That is a way.