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>

<pre><code>from random import randint

class newgame():

    ai_score = 0
    user_score = 0

    def __init__(self):
        self.ai_score = 0
        self.user_score = 0

    def playgame(self):
        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!")
                    self.ai_score += 1
                    break
                elif (user_guess == '1' and ai_guess == '3'):
                    print("AI Selected Scissors")
                    print("Rock Beats Scissors")
                    print("You Win!")
                    self.user_score += 1
                    break
                elif (user_guess == '2' and ai_guess == '1'):
                    print("AI Selected Rock")
                    print("Paper Beats Rock")
                    print("You Win!")
                    self.user_score += 1
                    break
                elif (user_guess == '2' and ai_guess == '3'):
                    print("AI Selected Scissors")
                    print("Scissors Beats Paper")
                    print("AI Wins!")
                    self.ai_score += 1
                    break
                elif (user_guess == '3' and ai_guess == '1'):
                    print("AI Selected Rock")
                    print("Rock Beats Scissors")
                    print("AI Wins!")
                    self.ai_score += 1
                    break
                elif (user_guess == '3' and ai_guess == '2'):
                    print("AI Selected Paper")
                    print("Scissors Beats Paper")
                    print("You Win!")
                    self.user_score += 1
                    break
                else:
                    pass
                    break

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

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


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

            if option == '1':
                game_object.playgame()
            elif option == '2':
                print("\nScores")
                print("Your Score: " + str(game_object.user_score))
                print("AI Score: " + str(game_object.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>Classes are wonderful. <code>__init__</code> is  the constructor for this class. It basically makes the object instant for the class and sets the variable to what you want. <code>game_object = newgame()</code> makes the class object and stores it into game_object. To get the class variable of the game_object, we use <code>game_object.ai_score</code>. Since you made a class object it's class variables are still in scope of the object you made, even though it might be outside of your function. Generally, if I need to use a variable outside of a function, and is tempted to use Global, I make a class instead. There are some cases where you wouldn't want this, but personally I haven't come across one. Also you might want to look into like what the comments were saying about using dictionary as options. Any other questions? </p>

<p>Edit: </p>

<p>To answer your new question about <code>print('%s is not an option' % option)</code> always running, is because in your code you had <code>if option == '1':</code> and then <code>if option == '2':</code> You want the option 2 to be elif. I fixed it in my code. If statements are in blocks. since you started a new if, the else didn't check first first if to see if it's a valid option. It was out of it's scope in a sense. So since your code was basically saying is option equal to 1? is it equal to 2 or 3 or anything else? notice these are two different questions? </p>

python