mercredi 10 juin 2020

Program functions when "elif" is used, but does not when "elif" is replaced with "if". I am having trouble figuring out why? (Python)

I am making a small rock paper scissors program in python. The following code is functional. When I replace the "elif" with "if" the program is not completely functional (returns the else statement often) and I do not understand why. I am aware that elif should be used, but I still do not understand why using "if" is causing a loss in functionality. Thanks!

import random
game = ("rock", "paper", "scissors")
userchoice = str(input("rock, paper, or scissors?"))
while (userchoice == "rock" or "paper" or "scissors"):
    computerchoice = random.choice(game)
    if (computerchoice == "rock" and userchoice == "scissors"):
        print("you lose",computerchoice,"beats",userchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == "scissors" and userchoice == "paper"):
        print("you lose",computerchoice,"beats",userchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == "paper" and userchoice == "rock"):
        print("you lose",computerchoice,"beats",userchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == "rock" and userchoice == "paper"):
        print("you win",userchoice,"beats",computerchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == "scissors" and userchoice == "rock"):
        print("you win",userchoice,"beats",computerchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == "paper" and userchoice == "scissors"):
        print("you win",userchoice,"beats",computerchoice)
        userchoice = str(input("try again, rock, paper, or scissors?"))
    elif (computerchoice == userchoice):
        print("tie")
        userchoice = str(input("try again, rock, paper, or scissors?"))
    else:
        print("what?")
        userchoice = str(input("try again, rock, paper, or scissors?"))

Aucun commentaire:

Enregistrer un commentaire