Score function in rock paper scissors python issue and others

pixe anzu :

I am new to programming & trying to create a best of 5 RPS game for my class but running into a few problems...when I call my score function that I created alternativley, it works but it then makes it so my while loop just keeps going, when I dont call the score function the while loops does stop at 3

 def gamerps():
        import random
        YS= 0
        CS= 0
        rules = {("R", "S"), ("P", "R"), ("S", "P")}
        msgs = {"R": "rock beats scissors", "P": "paper covers rock", "S":" scissors cut paper"}
        while YS or CS <= 3:
            YM = input("R/P/S?")
            CM = random.choice(["R", "P", "S"])
            if (YM, CM) in rules:
                print ("You won! %s" % (msgs[YM]))
                YS= YS+1
                print (scorerps(YS, CS))
            elif (CM, YM) in rules:
                print ("You lost! %s" % (msgs[CM]))
                CS= CS+1
                print (scorerps(YS, CS))
            elif YM == CM:
                print ("Tie! Go again!")
            else:
                print ("ERROR, please choose R, P, or S for [R]ock [P]aper [S]cissors")

gamerps()

the score function works fine on its own and it does add the score throughout the rps game when applied, it just makes the game no longer stop at first to 3....

def scorerps(YS, CS):
        if YS==CS:
            print("scores are tied at", YS, "-", CS, "!")
        elif YS >CS:
            if YS==3:
                print("You won ", YS, "-", CS,"!")
            else:
                print("You lead ", YS, "-", CS,"!")
        elif CS>YS:
            if CS==3:
                print("Computer won ", CS, "-", YS,"!")
            else:
                print("computer leads ", CS, "-", YS,"!")

    scorerps(1, 2)
Odysseus :

Check the condition of your while loop:

while YS or CS <= 3:

means that the loop is running as long as YS != 0 or CS <= 3 and this is probably not what you wanted.

You probably wanted the loop to run until one of the variables exceeds 3 so you have to use and anyway:

while YS <= 3 and CS <= 3:

=> This way the condition becomes False when one of the variables gets > 3

Keep in mind that this also means you need to win 4 rounds in order to win the game, because <= 3 is still True after one side won its 3rd round. So you might want to check for < 3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21334&siteId=1