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.
Here's what I have:
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()
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.
As a quick extra note, the second
else:
print('%s is not an option' % option)
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.