0

I've been having a bit of difficulty with my Python programme. It plays Rock Paper Scissors. The code is pretty large so I'll shorten it to a sample programme, but the full code can be viewed at https://github.com/Ph0enix0/doitall/blob/master/computerpungramming/rockpaperscissors.py

(count is the amount of games played)

count = 0
user_wins = 0
comp_wins = 0
def game():
 comp_answer = ['rock', 'paper', 'scissors']
 user_answer = input('Rock, paper, or scissors?')
 if user_answer = 'rock':
 count += 1
 if comp_answer = 'paper':
 print('Paper! I win!)
 comp_wins += 1
#The rest of the comparisons are here
while 1 == 1:
game()

If I put the variables in game() they will reset every time. Help?

asked Mar 31, 2015 at 0:28
4
  • 1
    First, in your conditions you should use ==, not =. Commented Mar 31, 2015 at 0:30
  • Where is the error???? Commented Mar 31, 2015 at 0:31
  • You need to close the 'Paper! I win! string Commented Mar 31, 2015 at 0:32
  • Please post the code that you are running and the traceback that you are getting. The code snippet that you have included is just rubbish Commented Mar 31, 2015 at 0:56

3 Answers 3

2

You need to move the variables inside the function and define scissors_wins:

def game():
 '''
 The actual game code. The code randomly chooses
 rock, paper, or scissors. Then, it checks if the user's choice
 beats the computer's. If it does, the user wins, and vice versa.
 '''
 #User picks rock, paper, or scissors.
 user_answer = input('Rock, paper, or scissors? ')
 #Tell the code that the game is not finished. Used for while loops.
 game_finished = False
 count = 0
 user_wins = 0
 comp_wins = 0
 scissors_wins = 0

Your game already has a loop so unless you are doing something in your while 1 == 1 then simply call game()

And yes every time you call the function the variables will be reset to 0.

If you want the win count to persist through many runs use a dict declaring outside the function:

d = {"user":0,"comp":0,"scissor":0}

Then update at the end of each game:

 d["user"] += user_wins 

Or increase the count of each inside the function instead of using the variables.

You also don't seem to ever break the loop so your code will loop infinitely. I imagine you want to also ask the user more than once for input so move user_answer = input('Rock, paper, or scissors? ') inside the loop.

Something like the following:

d = {"user":0,"comp":0,"scissor":0}

d = {"user":0,"comp":0,"scissor":0}

def game():
 '''
 The actual game code. The code randomly chooses
 rock, paper, or scissors. Then, it checks if the user's choice
 beats the computer's. If it does, the user wins, and vice versa.
 ''' 
 #Tell the code that the game is not finished. Used for while loops.
 game_finished = False
 count = 0
 user_wins = 0
 comp_wins = 0
 scissors_wins = 0
 #Game isn't finished, so the code continues.
 while not game_finished:
 user_answer = input('Rock, paper, or scissors? ')
 # update dict with totals when loop breaks
answered Mar 31, 2015 at 0:33
Sign up to request clarification or add additional context in comments.

Comments

1

The game function can't change the variables outside it (count, user_wins, comp_wins). Have the function return the necessary values.

count = 0
user_wins = 0
comp_wins = 0
def game():
 ...
 return count, user_wins, comp_wins
count, user_wins, comp_wins = game()
answered Mar 31, 2015 at 0:34

3 Comments

Or use globals keyword
"function doesn't know about the variables " - it knows about them. It just cant write to them without global keyword.
This answer works! Thank you very much, TigerhawkT3. I'm a tad new to Python and I haven't programmed for a month, so I'm a bit rusty.
0

You're going to want to change

if comp_answer = 'paper':

to

if comp_answer == 'paper':

That's probably what's throwing the error. Change the = to == in any other places where you're checking an if condition.

Nic
13k20 gold badges85 silver badges107 bronze badges
answered Mar 31, 2015 at 0:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.