Python - Problem printing inside a loop

Asked

Viewed 496 times

2

I have the following code Python, representative of the game Pedra,Papel,Tesoura.

import random
listChoices = ["rock","paper","scissor"]
print("Choose rock, paper or scissor to play or write exit to leave the game")
userChoice = str.lower(input("Rock,Paper or Scissor: "))

for choice in listChoices:
if userChoice != choice:
    print("You have to choose one of the this three options to play: rock, paper or scissor or exit to leave the game")
    break

pcChoice = random.choice(listChoices)

while userChoice != "exit":
if(userChoice == pcChoice):
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("It's a tie")
elif ((userChoice == "rock" and pcChoice == "scissor") or (userChoice == "scissor" and pcChoice == "paper") or (userChoice == "paper" and pcChoice == "rock") ):
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("User Wins congrats!!!!! :)")
elif ((pcChoice == "rock" and userChoice == "scissor") or (pcChoice == "scissor" and userChoice == "paper") or (pcChoice == "paper" and userChoice == "rock") ):
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("Pc Wins Try Again!!!!! :(")
break

The problem is this every time I run the program and if the user does not write what the program asks for (rock,paper ou sciassor) it prints the error message and terminates the program. On the other hand if it prints one of these three options apart from running the block that is inside the while, also prints the previously described error message.
Thanks in advance.

  • I also created a game like this, read my code and try to understand your problem: https://repl.it/Fiza/0

1 answer

3


You were going through the list of possible choices, and comparing each value ('rock', 'paper', Scissor) with what the user put, now he would always stop at something that the user had not put. That’s how I think you want it:

import random
listChoices = ["rock","paper","scissor"]
print("Choose rock, paper or scissor to play or write exit to leave the game")

userChoice = str.lower(input("Rock,Paper or Scissor: "))
while userChoice not in listChoices:
    print("You have to choose one of the this three options to play: rock, paper or scissor or exit to leave the game")
    userChoice = str.lower(input("Rock,Paper or Scissor: "))

pcChoice = random.choice(listChoices)

if(userChoice == pcChoice):
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("It's a tie")
elif ((userChoice == "rock" and pcChoice == "scissor") or (userChoice == "scissor" and pcChoice == "paper") or (userChoice == "paper" and pcChoice == "rock") ):
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("User Wins congrats!!!!! :)")
else:
    print("User Choice --> ",(userChoice))
    print("Pc Choice --> ",(pcChoice))
    print("Pc Wins Try Again!!!!! :(")

And here’s a way to keep repeating until the user puts "Exit":

import random

listChoices = ["rock","paper","scissor"]
print("Choose rock, paper or scissor to play or write exit to leave the game")

userChoice = str.lower(input("Rock,Paper or Scissor: "))
while True:
    while userChoice not in listChoices:
        if userChoice == 'exit':
            break
        print("You have to choose one of the this three options to play: rock, paper or scissor or exit to leave the game")
        userChoice = str.lower(input("Rock,Paper or Scissor: "))

    else: # isto acontece se nao tiver havido break em cima
        pcChoice = random.choice(listChoices)

        if(userChoice == pcChoice):
            print("User Choice --> ",(userChoice))
            print("Pc Choice --> ",(pcChoice))
            print("It's a tie")
        elif ((userChoice == "rock" and pcChoice == "scissor") or (userChoice == "scissor" and pcChoice == "paper") or (userChoice == "paper" and pcChoice == "rock") ):
            print("User Choice --> ",(userChoice))
            print("Pc Choice --> ",(pcChoice))
            print("User Wins congrats!!!!! :)")
        else:
            print("User Choice --> ",(userChoice))
            print("Pc Choice --> ",(pcChoice))
            print("Pc Wins Try Again!!!!! :(")

        userChoice = None
        continue
    break

Browser other questions tagged

You are not signed in. Login or sign up in order to post.