Home > Enterprise >  while loop needs to repeat when asked
while loop needs to repeat when asked

Time:07-25

so for this assignment I am needed to play a game of rock paper scissors. I have all of it done except for when I want to start a new game the scores don't go back to zero. The basics of my code are:

    x=1
    i=1
    ps=0
    cs=0
    while i==1:
        while x==1:
            print("First to 5 wins!")
            #print score
            score(str(ps),str(cs))

            #code for game and the outcomes

            #ending
            if ps!=5 and cs!=5:
                end=yesorno("Would you like to forfeit? (y/n)")
                if end=="y":
                    print("You coward!")
                    ngc=yesorno("Would you like to start a new game? (y/n)")
                    ng=ngc.lower()
                    if ng=="y":
                        i=2
                    else:
                        i=1
                        x=2
                        input("press enter to exit...")
                        
                else:
                    i=1
            else:
                if cs==5:
                    print("The computer won 5 to " ps "...")
                    ngc=yesorno("Would you like to start a new game? (y/n)")
                    ng=ngc.lower()
                    if ng=="y":
                        i=2
                    else:
                        i=1
                        x=2
                        input("press enter to exit...")
                else:
                    print("The player won 5 to " str(cs) "...")
                    ngc=yesorno("Would you like to start a new game? (y/n)")
                    ng=ngc.lower()
                    if ng=="y":
                        i=2
                    else:
                        i=1
                        x=2
                        input("press enter to exit...")

CodePudding user response:

When you ask if a player wants to start a new game, you should change i to 1, instead of 2.

In addition, it's possible that you have a mistake in yesorno function.

But I recommend you change the structure of your code. Create a function, that starts a new game, for example. And please, use better names for variables:)

CodePudding user response:

It should work if everywhere you handle the new game prompt you put

if ng=="y":
    ps = 0
    cs = 0
    i=2

Maybe you are not far enough in to learning programming but this is a good opportunity to use a function to reduce code duplication.

As well as this, variables i and x seem like they could be booleans, like

while continueGame:
    ...
    # End game code
    continueGame = False

If you haven't learnt this stuff yet don't worry about it.

  • Related