-1

So I'm creating a small hangman game to practice my Python and before I code the "Y" or "Yes" conditional portion of my code I decided to test out the responses to make sure it takes in the user inputs correctly. Unfortunately when I enter "N" or "No" the code doesn't skip past the "Y" or "Yes" portion of my code and returns

"Welcome to a game of Hangman! Please type in Y or Yes to play or N or No to quit no Yay! I'm glad you decided to play! Hmm let me guess a word.................ok DONE! Please type in a letter or guess the word!"

....any ideas why it's not skipping the "Yes" portion of the code?

import random

list_of_words = ["banana", "monster", "development", "anarchy", "congruence", "display", "algorithm"]

word_chosen = random.choice(list_of_words)

print("Welcome to a game of Hangman! ")
user_response = str(input("Please type in Y or Yes to play or N or No to quit ")).lower()

if user_response == 'y' or 'yes':
    print("Yay! I'm glad you decided to play! Hmm let me guess a word.................ok DONE!")
    letter_guess = str(input("Please type in a letter or guess the word! ")).lower()
    
elif user_response == 'n' or 'no':
    print("Awww well come back next time if you'd like to play HANGMAN!!!! DUN DUN DUNNNN ")
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34

1 Answers1

1

your if conditions need some modifications:

use

if user_response == 'y' or user_response == 'yes':

instead of

if user_response == 'y' or 'yes':

and use

elif user_response == 'n' or user_response == 'no':

instead of

elif user_response == 'n' or 'no':

the problem is because the boolean value of a non-empty string is equal to True.

print(bool('yes'))
# True