AfterHover
AfterHover

Reputation: 107

make program restart if user typed smthing wrong and ask user if he wants to restart the game

This is the code i am currently running

from random import randint
print("Rock,Paper or Scissors?")
choice=input()
r=["Rock" , "Paper" , "Scissors"]
comp=r[randint(0,2)]

I have tried to change the lo to False or to True , but still can't figure it out

lo = True
while lo==True :
    if comp == choice:
        print("Tie!")
    elif comp == "Rock":
        if choice == "Paper":
            print("You won, " + choice + " covers " + comp )
        elif choice == "Scissors":
            print("You lost, " + comp + " breaks " + choice )
    elif comp == "Scissors":
        if choice == "Rock":
            print("You won, " + choice + " breaks " + comp )
        elif choice == "Paper":
            print("You lost, " + comp + " cuts " + choice)
    elif comp == "Paper":
        if choice == "Rock":
            print("You won, " + choice + " covers " + comp )
        elif choice == "Scissors":
            print("You won, " + choice + " cuts " + comp )
    else:
        print("You have entered something wrong please re-enter your choice")
print("Do you want to play again. Yes or No?")
answ=input()
if answ=="Yes":
    lo=True
    print("Choose between Rock,Paper and Scissors again")
    choice=input()
elif answ=="No":
    lo=False
    print("Goodbye!")

Also i don't know why , the output is just an indefinite loop

Example

Upvotes: 0

Views: 21

Answers (1)

Daniel Ben-Shabtay
Daniel Ben-Shabtay

Reputation: 350

It is an infinite loop because you don't change lo
The question is out of the while loop This a possible fix:

from random import randint

lo = True
while lo==True :
    err=False
    print("Rock,Paper or Scissors?")
    r=["Rock" , "Paper" , "Scissors"]
    comp=r[randint(0,2)]
    choice=input()
    if comp == choice:
        print("Tie!")
        lo=False
    elif comp == "Rock":
        if choice == "Paper":
            print("You won, " + choice + " covers " + comp )
        elif choice == "Scissors":
            print("You lost, " + comp + " breaks " + choice )
        lo=False
    elif comp == "Scissors":
        if choice == "Rock":
            print("You won, " + choice + " breaks " + comp )
        elif choice == "Paper":
            print("You lost, " + comp + " cuts " + choice)
        lo=False
    elif comp == "Paper":
        if choice == "Rock":
            print("You won, " + choice + " covers " + comp )
        elif choice == "Scissors":
            print("You won, " + choice + " cuts " + comp )
        lo=False
    else:
        print("You have entered something wrong please re-enter your choice")
        err=True
    if(not err):
        print("Do you want to play again. Yes or No?")
        answ=input()
        if answ=="Yes":
            lo=True
        elif answ=="No":
            lo=False
        else:
            lo=False

Upvotes: 1

Related Questions