Search code examples
pythonglobal-variablesglobal

"global" headache in Rock, Paper, Scissors game


I am trying to write python code related to water, snake and gun game (Rock, Paper, Scissor).

If I use this code:

import random
usscore = 0
pcscore = 0

wsg = ["Water", "Snake", "Gun"]
inp = [1,2,3]
wsginputT = 0
wsginpw = ""

def wsgf(wsginpw,compran,pcscore,wsginput,usscore):
    if wsginpw == compran:
        print("AARGH its a draw")
        pass
    else:
        if wsginput == 1 and compran == wsg[1]:#WATER VS SNAKE
            print("LOLLL")
            print("HAHA Snake drunk all the water")
            pcscore = pcscore +1
        else:
            if wsginput == 1 and compran == wsg[2]:#WATER VS GUN
                print("Hmmmm will see ya next time! ")
                print("Gun had no chance against WATER")
                usscore = usscore + 1
            else:
                if wsginput == 2 and compran == wsg[0]:#SNAKE VS WATER
                    print("Hmmmm will see ya next time! ")
                    print("HUH Your snake drunk all the water")
                    usscore = usscore + 1
                else:
                    if wsginput == 2 and compran == wsg[2]:#SNAKE VS GUN
                        print("LOLLL")
                        print("HAHA My Gun killed your snake")
                        pcscore = pcscore + 1
                    else:
                        if wsginput == 3 and compran == wsg[0]:#GUN VS WATER
                            print("LOLLL ")
                            print("HAHA your gun is drowned in water!")
                            pcscore = pcscore + 1
                        else:
                            if wsginput == 3 and compran == wsg[1]:#GUN VS SNAKE
                                print("Hmmmm will see ya next time!")
                                print("Not Fair my snake was killed by your gun")
                                usscore = usscore + 1
    print("Pc score",pcscore, "User Score", usscore)

for ten in range(10):
    while True:
        try:
            wsginput = int(input("""Please enter
                             1 FOR WATER
                             2 FOR SNAKE
                             3 FOR GUN\n"""))
            if wsginput in inp:
                break
            else:
                print("Please enter value between 1 and 3 inclusive")
                continue

        except:
            print("Please enter a valid integer")
            continue

    wsginpw = wsg[wsginput-1]
    # print(wsginpw)

    compran = random.choice(wsg)
    # print(compran)
    wsgf(wsginpw,compran,pcscore,wsginput,usscore)

print("Your score is ", usscore)
print("Computer's score is ", pcscore)
if usscore > pcscore:
    print("You Won")
else:
    print("You Lose")

The score I receive at end is 0. You can run it.

The problem solved by introducing global function and code runs smoothly but I remember someone saying not to use global in question Headbutting UnboundLocalError (blah…blah…blah)

The code with globals is as follows:

import random
usscore = 0
pcscore = 0

wsg = ["Water", "Snake", "Gun"]
inp = [1,2,3]
wsginputT = 0
wsginpw = ""

def wsgf():
    global wsginpw
    global compran
    global pcscore
    global wsginput
    global usscore

    if wsginpw == compran:
        print("AARGH its a draw")
        pass
    else:
        if wsginput == 1 and compran == wsg[1]:#WATER VS SNAKE
            print("LOLLL")
            print("HAHA Snake drunk all the water")
            pcscore = pcscore +1
        else:
            if wsginput == 1 and compran == wsg[2]:#WATER VS GUN
                print("Hmmmm will see ya next time! ")
                print("Gun had no chance against WATER")
                usscore = usscore + 1
            else:
                if wsginput == 2 and compran == wsg[0]:#SNAKE VS WATER
                    print("Hmmmm will see ya next time! ")
                    print("HUH Your snake drunk all the water")
                    usscore = usscore + 1
                else:
                    if wsginput == 2 and compran == wsg[2]:#SNAKE VS GUN
                        print("LOLLL")
                        print("HAHA My Gun killed your snake")
                        pcscore = pcscore + 1
                    else:
                        if wsginput == 3 and compran == wsg[0]:#GUN VS WATER
                            print("LOLLL ")
                            print("HAHA your gun is drowned in water!")
                            pcscore = pcscore + 1
                        else:
                            if wsginput == 3 and compran == wsg[1]:#GUN VS SNAKE
                                print("Hmmmm will see ya next time!")
                                print("Not Fair my snake was killed by your gun")
                                usscore = usscore + 1
    print("Pc score",pcscore, "User Score", usscore)

for ten in range(10):
    while True:
        try:
            wsginput = int(input("""Please enter
                             1 FOR WATER
                             2 FOR SNAKE
                             3 FOR GUN\n"""))
            if wsginput in inp:
                break
            else:
                print("Please enter value between 1 and 3 inclusive")
                continue

        except:
            print("Please enter a valid integer")
            continue

    wsginpw = wsg[wsginput-1]
    # print(wsginpw)

    compran = random.choice(wsg)
    # print(compran)
    wsgf()

print("Your score is ", usscore)
print("Computer's score is ", pcscore)
if usscore > pcscore:
    print("You Won")
else:
    print("You Lose")

Someone please find me a way to not to use globals and please remember I am a beginner and learning. If you use something difficult please explain it as well. Also, any tips on how to shorten my code?


Solution

  • If you don't use global you need to return the variables that you're assigning, so the caller can get the updated values.

    def wsgf(wsginpw,compran,pcscore,wsginput,usscore):
        if wsginpw == compran:
            print("AARGH its a draw")
            pass
        else:
            if wsginput == 1 and compran == wsg[1]:#WATER VS SNAKE
                print("LOLLL")
                print("HAHA Snake drunk all the water")
                pcscore = pcscore +1
            else:
                if wsginput == 1 and compran == wsg[2]:#WATER VS GUN
                    print("Hmmmm will see ya next time! ")
                    print("Gun had no chance against WATER")
                    usscore = usscore + 1
                else:
                    if wsginput == 2 and compran == wsg[0]:#SNAKE VS WATER
                        print("Hmmmm will see ya next time! ")
                        print("HUH Your snake drunk all the water")
                        usscore = usscore + 1
                    else:
                        if wsginput == 2 and compran == wsg[2]:#SNAKE VS GUN
                            print("LOLLL")
                            print("HAHA My Gun killed your snake")
                            pcscore = pcscore + 1
                        else:
                            if wsginput == 3 and compran == wsg[0]:#GUN VS WATER
                                print("LOLLL ")
                                print("HAHA your gun is drowned in water!")
                                pcscore = pcscore + 1
                            else:
                                if wsginput == 3 and compran == wsg[1]:#GUN VS SNAKE
                                    print("Hmmmm will see ya next time!")
                                    print("Not Fair my snake was killed by your gun")
                                    usscore = usscore + 1
        print("Pc score",pcscore, "User Score", usscore)
        return pcscore, usscore
    
    for ten in range(10):
        while True:
            try:
                wsginput = int(input("""Please enter 
                                 1 FOR WATER
                                 2 FOR SNAKE
                                 3 FOR GUN\n"""))
                if wsginput in inp:
                    break
                else:
                    print("Please enter value between 1 and 3 inclusive")
                    continue
    
            except:
                print("Please enter a valid integer")
                continue
    
        wsginpw = wsg[wsginput-1]
        # print(wsginpw)
    
        compran = random.choice(wsg)
        # print(compran)
        pcscore, usscore = wsgf(wsginpw,compran,pcscore,wsginput,usscore)