in

Adding a score function to Rock Paper Scissors Python


I have been trying to add a scoring system to keep track of the player and the computer’s score but it just isn’t working. Everything else is running smoothly. When I run the code it tells me that the variables aren’t defined. Here’s the code…

from random import random, randint
import random

running = True
while running == True:
    print("=====Rock Paper Scissors: The Python Game=====")
    print("n")

    def Score():
        global p_wins,p_losses,p_draws,c_wins,c_losses,c_draws

        p_wins = 0
        p_losses = 0
        p_draws = 0
        c_wins = 0
        c_losses = 0
        c_draws = 0

        print("====Player's Score====")
        print(f"Wins:{p_wins}")
        print(f"Losses:{p_losses}")
        print(f"Draws:{p_draws}")

        print("====CPU's Score====")
        print(f"Wins:{c_wins}")
        print(f"Losses:{c_losses}")
        print(f"Draws:{c_draws}")

    name = input("Player please enter you name:n")
    print("n")

    
    myItem = input("Enter Either Rock Paper or Scissors:n")


    if myItem not in ("Rock","Paper","Scissors"):
       print("Are you Ok!! I said enter rock paper or scissors")
       print("GAME OVER!!!!!!")
       break
    

#Computer's Choices
    computerItem = random.randint(1,3)
    if computerItem == 1:
        computerItem = "Rock"
        print(f"Computer chooses {computerItem}...")
    if computerItem == 2:
        computerItem = "Paper"
        print(f"Computer chooses {computerItem}...")
    if computerItem == 3:
        computerItem = "Scissors"
        print(f"Computer chooses {computerItem}...")


    if myItem == computerItem:
        print("Try Again!! You and the CPU picked the same thing")
        p_draws += 1
        c_draws += 1
        

#How I can loose to the CPU
    if myItem == "Rock" and computerItem == "Paper":
        print("[Paper covers Rock]")
        print("CPU WINS!!!")
        p_losses += 1
        c_wins += 1
        Score()
        
    if myItem == "Paper" and computerItem == "Rock":
        print("[Paper covers Rock]")
        print(f"{name} WINS!!!")
        p_wins += 1
        c_losses += 1
        Score()

    if myItem == "Scissors" and computerItem == "Rock":
        print("[Rock Smashes Scissors]")
        print("CPU WINS!!!")
        p_losses += 1
        c_wins += 1
        Score()
        
    if myItem == "Rock" and computerItem == "Scissors":
        print("[Rock smashes Scissors]")
        print(f"{name} WINS!!!")
        p_wins += 1
        c_losses += 1
        Score()
        

    if myItem == "Paper" and computerItem == "Scissors":
        print("[Scissors cut Paper]")
        print("CPU WINS!!!")
        p_losses += 1
        c_wins += 1
        Score()
        

    if myItem == "Scissors" and computerItem == "Paper":
        print("[Scissors cut Paper]")
        print(f"{name} WINS!!!")
        p_wins += 1
        c_losses += 1
        Score()

        nextRound = 0
        

    quitGame = input("Do you want to continue playing (Y/N):n")
    if quitGame == "N":
         break
    if quitGame =="Y":
         nextRound = 1
         nextRound += 1
         print(f"Round({nextRound})")

Does it maybe have something to do with the indentation?



Source: https://stackoverflow.com/questions/70536280/adding-a-score-function-to-rock-paper-scissors-python

HashiCorp Boundary 0.7 and Boundary Desktop 1.4 Released with Dynamic Host Catalogs

Vuetify datetime picker with input fields