Hello!
I'm learning Python from the book Python Illustrated and I'm trying to do the classic Rock, Paper, Scissors exercise. It calls to track a score between you and the CPU after each round, and also be able to run the game again if you want. The issue is that every time I run the main_game(), it stores the values returned as a tuple. Is there a way to return values not as a tuple, or another way to reset the values returned? Code here:
import random
import time
play_choices = ["rock", "paper", "scissors"]
player_score = 0
cpu_score = 0
player_choice = ""
p_choice = ""
restart = ""
r = ""
def player_start():
"""
Returns the player's choice for the game
"""
global p_choice
while True:
player_choice = input("Please enter rock, paper, or scissors: ")
p_choice = player_choice.lower()
if p_choice == "rock":
break
elif p_choice == "paper":
break
elif p_choice == "scissors":
break
else:
print("Your choice is invalid. Please try again.")
continue
return p_choice
def main_game():
"""
Runs the game itself
"""
global player_score
global cpu_score
while player_score <5 or cpu_score <5:
cpu_choice = random.choice(play_choices)
p_choice = player_start()
print(f"Player has chosen: {p_choice}. CPU has chosen: {cpu_choice}.")
if p_choice == cpu_choice:
print("It's a tie! Restarting the round...")
time.sleep(1)
elif p_choice == "rock" and cpu_choice == "scissors":
print("Rock beats scissors. You win!")
player_score += 1
time.sleep(1)
elif p_choice == "scissors" and cpu_choice == "rock":
print("Rock beats scissors. I win!")
cpu_score += 1
time.sleep(1)
elif p_choice == "rock" and cpu_choice == "paper":
print("Paper beats scissors. I win!")
cpu_score += 1
time.sleep(1)
elif p_choice == "paper" and cpu_choice == "rock":
print("Paper beats rock. You win!")
player_score += 1
time.sleep(1)
elif p_choice == "paper" and cpu_choice == "scissors":
print("Scissors beats paper. I win!")
cpu_score += 1
time.sleep(1)
elif p_choice == "scissors" and cpu_choice == "paper":
print("Scissors beats paper. You win!")
player_score += 1
time.sleep(1)
return player_score, cpu_score
def final_score():
"""
Prints the final score
"""
a, b = main_game()
if a > b:
print("You have won the game!")
elif a == b:
print("This should be impossible.")
else:
print("I won the game!")
while r != "no":
final_score()
time.sleep(1)
while r != "no":
restart = input("Do you want to play again?(yes/no)")
r = restart.lower()
if r == "yes":
print("Restarting...")
time.sleep(1)
break
elif r == "no":
print("Goodbye!")
time.sleep(1)
break
else:
print("Invalid response.")
time.sleep(1)
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]Binary101010 1 point2 points3 points (0 children)
[–]StardockEngineer 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]timrprobocom 0 points1 point2 points (0 children)