Python - Problem when doing print within a loop

3

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 the following 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 the program terminates. On the other hand, if it prints one of these three options in addition to executing the block that is within while , it also prints the previously described error message. Thank you in advance.

    
asked by anonymous 14.02.2017 / 04:14

1 answer

4

You were going through the list of possible choices, and comparing each value ('rock', 'paper', scissor) with what the user put in, sometimes so he would always stop at something that the user had not put . This is 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 is 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
    
14.02.2017 / 09:34