dimanche 10 novembre 2019

If, else, and elif in Python 3 - Why is my ELSE command always executing? [closed]

Why is my else command being called despite one of the 3 large if statements being true. I thought that ELSE only gets executed if none of the prior if/elif statements are true, what am I missing here?

import random

while True:
    computer = random.choice(["rock", "paper", "scissors"])
    user_input = "rock"

    user_input = input("Rock, Paper, or Scissors? \n Write your weapon of choice here: ")
    user_input = user_input.lower()

    if user_input == "rock":
        if computer == "rock":
            print("You both chose rock!")
        if computer == "paper":
            print("You put rock to paper, you lost!")
        if computer == "scissors":
            print("You rocked the computer's scissors, you won!")

    if user_input == "paper":
        if computer == "rock":
            print("You put paper over the computer's rock, you won!")
        if computer == "paper":
            print("You both chose paper, it's a tie!")
        if computer == "scissors":
            print("You chose paper into scissors... You lost!")

    elif user_input == "scissors":
        if computer == "rock":
            print("The computer rocked your scissors, you lost!")
        if computer == "paper":
            print("You cut up the computer's paper, you won!")
        if computer == "scissors":
            print("You both chose scissors, it's a tie!")
    else:
        print("Sorry I don't understand, please choose either 'rock' 'paper' or 'scissors'")

here is the output:

C:\Users\Darkm\PycharmProjects\TestProjects\venv\Scripts\python.exe C:/Users/Darkm/PycharmProjects/TestProjects/RockPaperScissorsGame.py
Rock, Paper, or Scissors? 
 Write your weapon of choice here: rock
You put rock to paper, you lost!
Sorry I don't understand, please choose either 'rock' 'paper' or 'scissors'
Rock, Paper, or Scissors? 
 Write your weapon of choice here: 

Aucun commentaire:

Enregistrer un commentaire