After spending a few months learning python I decided to build a basic Rock Paper Scissors game.
Basic construct of the game:
- Allows you to pick best of 3,5,7
- Allows you to put in the name
- Computer choices are completely random
I know there are probably a million different ways to write this code. But based on the way that I built it, are there any ways to make my code more efficient?
# Rock Paper Scissors
import random as rdm
print("Welcome to Rock/Paper/Scissors!!! \n")
gl = input("Would you like to play a best of 3, 5 or 7: ")
while gl not in ["3", "5", "7"]:
gl = input("Incorrect Response, please select 3, 5, or 7: ")
gl = int(gl)
human_1 = input("Please enter your name: ")
GameOptions = ['Rock', 'Paper', 'Scissors']
hmn_score = 0
cpt_score = 0
rps_running = True
def rps():
global cpt_score, hmn_score
while rps_running:
hmn_temp = input("""Please select from the following:
1 - Rock
2 - Paper
3 - Scissors
\n""")
while hmn_temp not in ["1", "2", "3"]:
print("That was not a acceptable input!")
hmn_temp = input("""Please select from the following:
1 - Rock
2 - Paper
3 - Scissors
\n""")
hmn_final = int(hmn_temp) - 1
print('You Chose: ' + GameOptions[hmn_final])
cpt = rdm.randint(0, 2)
print('Computer Chose: ' + GameOptions[cpt] + '\n')
if hmn_final == cpt:
print('Tie Game!')
elif hmn_final == 0 and cpt == 3:
print('You Win')
hmn_score += 1
elif hmn_final == 1 and cpt == 0:
print('You Win')
hmn_score += 1
elif hmn_final == 2 and cpt == 1:
print('You Win')
hmn_score += 1
else:
print('You Lose')
cpt_score += 1
game_score()
game_running()
def game_score():
global cpt_score, hmn_score
print(f'\n The current score is {hmn_score} for you and {cpt_score} for the computer \n')
def game_running():
global rps_running, gl
if gl == 3:
if hmn_score == 2:
rps_running = False
print(f"{human_1} Wins!")
elif cpt_score == 2:
rps_running = False
print(f"Computer Wins!")
else:
rps_running = True
elif gl == 5:
if hmn_score == 3:
rps_running = False
print(f"{human_1} Wins!")
elif cpt_score == 3:
rps_running = False
print(f"Computer Wins!")
else:
rps_running = True
elif gl == 7:
if hmn_score == 4:
rps_running = False
print(f"{human_1} Wins!")
elif cpt_score == 4:
rps_running = False
print(f"Computer Wins!")
else:
rps_running = True
rps()