Reputation: 5
import random
words = ["Rock", "Paper", "Scissors"]
wins, loss = 0, 0
while True:
word = random.choice(words)
ur_word = input("Rock, Paper, Scissors..: ")
print("\nComputer: {0}\nYou: {1}\n".format(word, ur_word))
if word == ur_word:
print("Tie!")
continue
elif ur_word == 'q':
break
elif word == 'Rock':
if ur_word == 'Paper':
print("You won!")
wins += 1
elif ur_word == 'Scissors':
print("You lost!")
loss += 1
elif word == 'Paper':
if ur_word == 'Rock':
print("You lost")
loss += 1
elif ur_word == 'Scissors':
print("You won!")
wins += 1
elif word == 'Scissors':
if ur_word == 'Rock':
print("You won!")
wins += 1
elif ur_word == 'Paper':
print("You lost!")
loss += 1
else: <<<<---------///----------HERE------------///----
print("Please enter a valid input.")
if wins == 3:
print("\n\tYou won the entire game of three rounds!")
break
elif loss == 3:
print("\n\tYou lost the entire game of three rounds!")
break
**Now, my question is: why does the selected else statement not execute when I enter an arbitrary value like "74n9wndwekf"?
A guy told me that you cannot execute if
and elif
statements after an else
statement, and also that you cannot execute your code after a break
statement. I don't understand what he meant and I don't know whether this information is of any value to you.**
Upvotes: 0
Views: 630
Reputation: 6902
In your if statement, you are checking the following:
The problem here is that the computer's random choice will always be either "Rock", or "Paper", or "Scissors", so one of the first three if/elif conditions will be met, therefore your else block will never be reached.
Try designing your if statements to check for the user input first instead of the computer's choice.
Upvotes: 0
Reputation: 71424
The else
never happens because one of your elif
s will always be true. word
will always be one of Rock
, Paper
, or Scissors
so you'll never hit that else
.
A simple fix would be to restructure that chain of if...elif
to check ur_word
first.
Another way to go would be to validate the input before you do anything else, e.g.:
ur_word = input("Rock, Paper, Scissors..: ")
if ur_word not in {'Rock', 'Paper', 'Scissors', 'q'}:
print("Please enter a valid input.")
continue
Upvotes: 3