-1

I created the loss() , win() and draw() function to change the score following the events .The problem is that they are not getting called in the while loop to change the score and set it correctly. I had the score lines under every if and elif statement but decided to use a function to make the code shorter and more efficient.

    from random import randint
    print("Welcome to this game of rock paper scissors against the computer.")
    print("Good luck and have fun! Enjoy this game! ")
    print("To stop playing, type quit when the round ends.If you want to continue playing, press any key ")
    scoreH=0
    scoreAI=0
    Round=0

   def loss():
        scoreH=scoreH+0
        scoreAI=scoreAI+1
    def win():
        scoreH=scoreH+1
        scoreAI=scoreAI+0
    def draw():
        scoreH=scoreH+0
        scoreAI=scoreAi+0        

    x=True


    while x == True :
        # Choice
        Round=Round+1
        print("Round",Round,"is going to start. ")
        print("1.Rock")
        print("2.Paper")
        print("3.Scissors")
        choice=input("Enter your choice: ").lower()
        IntChoice=randint(1,3)


        #Transforming AI_choice from an int to a string 
        if IntChoice == 1:
            AI_choice="rock"
        elif IntChoice == 2:
            AI_choice="paper"
        elif IntChoice == 3:
            AI_choice="scissors"

        print(AI_choice)

        # Draw
        if choice==AI_choice:
            draw()
            print("The computer chose",choice,".You draw.")
        # PlayerWins
        elif choice=="paper" and AI_choice=="rock":
            win()
            print("The computer chose",choice,".You win.")
        elif choice=="scissors" and AI_choice=="paper":
            win()
            print("The computer chose",AI_choice,".You win.")
        elif choice=="rock" and AI_choice=="scissors":
            win()
            print("The computer chose",AI_choice,".You win.")
        # AIwins
        elif AI_choice=="paper" and choice=="rock":
            loss()
            print("The computer chose",AI_choice,".You lose.")
        elif AI_choice=="scissors" and choice=="paper":
            loss()
            print("The computer chose",AI_choice,".You lose.")
        elif AI_choice=="rock" and choice=="scissors":
            loss()
            print("The computer chose",AI_choice,".You lose.")
        else:
            print("Invalid input.")
            continue
        end=input()
        # Stop Playing
        if end == "stop" or end == "quit":
            print("The score is :",scoreH,"-",scoreAI)
            if scoreH > scoreAI:
                print("You win.")
            elif scoreH < scoreAI:
                print("You lose.")
            else:
                print("You draw.")
            break

        else:
            continue
khelwood
  • 55,782
  • 14
  • 81
  • 108
IskandarG
  • 1
  • 1
  • 3
  • "Not called"? I get `UnboundLocalError: local variable 'scoreH' referenced before assignment`, which clearly indicates the functions *are* called. – Jongware Apr 29 '20 at 09:54
  • Duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Jongware Apr 29 '20 at 09:55

2 Answers2

0

They are indeed being called but they are modifying the local variable inside themselves.

To modify the values do something like this:

def loss(scoreAI):
     return scoreAI+1


scoreAI = loss(scoreAI)

Actually, the functions are really simple and in some cases you're adding up 0 which is the same as doing nothing, so I wouldn't make functions for this. This is much simpler:

#Win
scoreH += 1

#Loss
scoreAI += 1

and you need nothing for draw.

I hope it helps!

Gamopo
  • 1,600
  • 1
  • 14
  • 22
-1
import random
print("Welcome to this game of rock paper scissors against the computer.")
print("Good luck and have fun! Enjoy this game! ")
print("To stop playing, type quit when the round ends.If you want to continue playing, press any key ")

scoreH=0
scoreAI=0
Round=0

def loss():
    global scoreH
    global scoreAI
    scoreH=scoreH+0
    scoreAI=scoreAI+1
def win():
    global scoreH
    global scoreAI
    scoreH=scoreH+1
    scoreAI=scoreAI+0
def draw():
    global scoreH
    global scoreAI
    scoreH=scoreH+0
    scoreAI=scoreAI+0




x=True


while x == True :
    # Choice
    Round=Round+1
    print("Round",Round,"is going to start. ")
    print("1.Rock")
    print("2.Paper")
    print("3.Scissors")
    choice=input("Enter your choice: ").lower()
    IntChoice=random.randint(1,3)


    #Transforming AI_choice from an int to a string
    if IntChoice == 1:
        AI_choice="rock"
    elif IntChoice == 2:
        AI_choice="paper"
    elif IntChoice == 3:
        AI_choice="scissors"

    print(AI_choice)

    # Draw
    if choice==AI_choice:
        draw()
        print("The computer chose",choice,".You draw.")
    # PlayerWins
    elif choice=="paper" and AI_choice=="rock":
        win()
        print("The computer chose",choice,".You win.")
    elif choice=="scissors" and AI_choice=="paper":
        win()
        print("The computer chose",AI_choice,".You win.")
    elif choice=="rock" and AI_choice=="scissors":
        win()
        print("The computer chose",AI_choice,".You win.")
    # AIwins
    elif AI_choice=="paper" and choice=="rock":
        scoreH,scoreAI=loss(scoreH,scoreAI)
        print("The computer chose",AI_choice,".You lose.")
    elif AI_choice=="scissors" and choice=="paper":
        loss()
        print("The computer chose",AI_choice,".You lose.")
    elif AI_choice=="rock" and choice=="scissors":
        loss()
        print("The computer chose",AI_choice,".You lose.")
    else:
        print("Invalid input.")
        #continue
        end=input()
        # Stop Playing
        if end == "stop" or end == "quit":
            x=False
            print("The score is :",scoreH,"-",scoreAI)
            if scoreH > scoreAI:
                print("You win.")
            elif scoreH < scoreAI:
                print("You lose.")
            else:
                print("You draw.")
                break

        else:
            continue

Expected Output:

Welcome to this game of rock paper scissors against the computer.
Good luck and have fun! Enjoy this game! 
To stop playing, type quit when the round ends.If you want to continue playing, press any key 
Round 1 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 2 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
paper
The computer chose paper .You draw.
Round 3 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: scissors
paper
The computer chose paper .You win.
Round 4 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 5 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 6 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
scissors
The computer chose scissors .You lose.
Round 7 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
scissors
The computer chose scissors .You lose.
Round 8 is going to start. 
1.Rock
2.Paper
3.Scissors
Enter your choice: 
paper
Invalid input.
stop
The score is : 4 - 2
You win.
Joshua
  • 136
  • 14