This is a simple rock, paper scissors game I attempted as part of the Automate the Boring Stuff with Python course on Udemy. The scope of the game is for the user to enter a valid input, the system then returns the result and the current score. If the user wishes to quit the game he/she enters 'q'
So far I have covered the following topics:
- Flow Control (Includes If, Elif, Else statements & While/For loops)
- Functions (Includes pythons built in functions, writings ones own function & local & global scopes)
- Lists
Considering what I have studied so far, I would like to get feedback on the following code:
import sys, random #Importing sys & random modules
print ('ROCK, PAPER, SCISSORS Game')
wins, losses, draws = 0, 0, 0
def turn_generation():
global turn #Making "turn" a global variable
global computer_turn #Making "computer_turn" a global variable
turn = random.randint(1,3)
if turn == 1:
computer_turn = 'r'
elif turn == 2:
computer_turn = 'p'
elif turn == 3:
computer_turn = 's'
while True: #Main loop of the game which asks the player to enter an input
print(str(wins) + ' Wins || ' + str(losses) + ' Losses || ' + str(draws) + ' Draws')
try:
players_turn = input('Enter your move: (r)ock (p)aper (s)cissors or (q)uit ')
turn_generation()
if players_turn == computer_turn: #In case both the moves by computer and player are same, this will execute.
print('It is a draw!')
draws += 1
elif players_turn == 'r' and computer_turn == 's':
print('Player wins!')
wins += 1
elif players_turn == 'p' and computer_turn == 'r':
print('Player wins!')
wins += 1
elif players_turn == 's' and computer_turn == 'p':
print('Player wins!')
wins += 1
elif players_turn == 'p' and computer_turn == 's':
print('Computer wins!')
losses += 1
elif players_turn == 's' and computer_turn == 'r':
print('Computer wins!')
losses += 1
elif players_turn == 'r' and computer_turn == 'p':
print('Computer wins!')
losses += 1
elif players_turn == 'q':
sys.exit() #Ending program if player enters "q"
except:
print('Incorrect value entered')
I would like to know if this program can be optimized in anyway within the topics I have covered.