How Would I Go About Making My Python Scoring System Work?
<p>I've been learning through an online course and I was trying to come up with ideas for things I could create to "test" myself as it were so I came up with a rock paper scissors game. It was working well so I decided to try and add a way of keeping track of your score vs the computer. Didn't go so well.</p>

<p>Here's what I have:</p>

<pre><code>from random import randint

ai_score = 0
user_score = 0

def newgame():
    print('New Game')
    try:
        while(1):
            ai_guess = str(randint(1,3))
            print('\n1) Rock \n2) Paper \n3) Scissors')
            user_guess = input("Select An Option: ")

            if(user_guess == '1'):
                print('\nYou Selected Rock')

            elif(user_guess == '2'):
                print('\nYou Selected Paper')

            elif(user_guess == '3'):
                print('\nYou Selected Scissors')

            else:
                print('%s is not an option' % user_guess)

            if(user_guess == ai_guess):
                print('Draw - Please Try Again')
            elif (user_guess == '1' and ai_guess == '2'):
                print("AI Selected Paper")
                print("Paper Beats Rock")
                print("AI Wins!")
                ai_score += 1
                break
            elif (user_guess == '1' and ai_guess == '3'):
                print("AI Selected Scissors")
                print("Rock Beats Scissors")
                print("You Win!")
                user_score += 1
                break
            elif (user_guess == '2' and ai_guess == '1'):
                print("AI Selected Rock")
                print("Paper Beats Rock")
                print("You Win!")
                user_score += 1
                break
            elif (user_guess == '2' and ai_guess == '3'):
                print("AI Selected Scissors")
                print("Scissors Beats Paper")
                print("AI Wins!")
                ai_score += 1
                break
            elif (user_guess == '3' and ai_guess == '1'):
                print("AI Selected Rock")
                print("Rock Beats Scissors")
                print("AI Wins!")
                ai_score += 1
                break
            elif (user_guess == '3' and ai_guess == '2'):
                print("AI Selected Paper")
                print("Scissors Beats Paper")
                print("You Win!")
                user_score += 1
                break
            else:
                pass
                break

    except KeyboardInterrupt:
        print("\nKeyboard Interrupt - Exiting...")
        exit()

#1 = Rock, 2 = Paper, 3 = Scissors


def main():
    while(1):
        print("\n1) New Game \n2) View Score \n3) Exit")
        try:
            option = input("Select An Option: ")

            if option == '1':
                newgame()   
            if option == '2':
                print("\nScores")
                print("Your Score: " + str(user_score))
                print("AI Score: " + str(ai_score))
            elif option == '3':
                print('\nExiting...')
                break
            else:
                print('%s is not an option' % option)
        except KeyboardInterrupt:
            print("\nKeyboard Interrupt - Exiting...")
            exit()


main()
</code></pre>

<p>I read somewhere that global variables can work but are generally frowned upon. Not sure why but then I can't say they're =0 so couldn't get that to work. Putting the ai_score and user_score in the newgame() doesn't work because it sets it to 0 every time you re run. Any help would be much appreciated.</p>

<p>As a quick extra note, the second</p>

<pre><code>else:
   print('%s is not an option' % option)
</code></pre>

<p>in main() always seems to execute and always says "1 is not an option" and I have no idea why it does that. I would assume something to do with while loops but I need those to keep it running so an explanation of why and how to fix would be great. At the end of the day, I'm just here to learn more.</p>

<p>At least one problem is this: your main has if...if...elif...else. The second if probably needs to be an elif. Tip: When you have a flow-of-control problem, put print statements inside each control branch, printing out the control variable and everything else that might possibly be relevant. This tells you which branch is being taken -- in this case, which branches, plural.</p>

<p>You don't say exactly what the problem was with keeping score, but I imagine it was an exception along the lines of variable referenced before assignment. If so, you should put "global ai_score" somewhere at the top of the function. What's going on is that Python can, but doesn't like to, recognize variables outside a function that are being used inside the function. You have to push a little. Consider:</p>

<pre><code>&gt;&gt;&gt; bleem = 0
&gt;&gt;&gt; def incrbleem():
...   bleem += 1
...
&gt;&gt;&gt; incrbleem()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 2, in incrbleem
UnboundLocalError: local variable 'bleem' referenced before assignment
&gt;&gt;&gt; def incrbleem():
...   global bleem
...   bleem += 1
...
&gt;&gt;&gt; bleem
0
&gt;&gt;&gt; incrbleem()
&gt;&gt;&gt; bleem
1
</code></pre>

<p>By the way, your code isn't bad at all, for a newbie.  I've seen much, much worse! For what it's worth, I don't think global variables are bad for a small, throw-away program like this. Once you have two programmers, or two threads, or two months between work sessions on the program, globals can definitely cause problems.</p>

python