import random
#create a sequence of words to choose from
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
#pick one word randomly from the sequence
word = random.choice(WORDS)
#create a variable to use later to see if the guess is correct
correct = word
#create a jumbled version of the word
jumble = ""
count = 1
score = 100
hint = ""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position+1):]
#start the game
print ("""
Welcome to Word Jumble!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
""")
print("The jumble is: ", jumble)
guess = input("Your guess: ").lower()
while(guess != correct) and (guess != ""):
print("Sorry, that's not it.")
guess = input("Your guess: ").lower()
count += 1
if count == 3:
option = input("Would you like a hint? (yes/no)" + '\n').lower()
if option == "yes":
hint = "yes"
if word == "python":
print("HINT: This was used to make this game.")
elif word == "jumble":
print("HINT: The name of the game.")
elif word == "easy":
print("HINT: Part of the Staples slogan.")
elif word == "difficult":
print("HINT: A synonym for hard.")
elif word == "answer":
print("HINT: Opposite of question.")
elif word == "xylophone":
print("HINT: An instrument.")
if guess == correct:
print("That's it! You guess it!\n")
score = score - 5*count
if hint == "yes":
score = score - 20
print("Thanks for playing. The word was", correct, ".")
print("You score is: " + str(score))
input("\n\nPress the enter key to exit.")
So i made this little game in python that basically asks the user to unscramble words, I'm having issues executing all the if statements. When i run my program, it works fine up to the part where it asks the user for a hint. Upon answering yes for a hint, it loops back into the while loop and instead of displaying the hint it just says "Sorry, that's not it" and continues going through the loop until the user gets the correct answer. So the following if statement in the code isn't being executed, can somebody explain why this is and how I can fix it?
if option == "yes":
hint = "yes"
if word == "python":
print("HINT: This was used to make this game.")
elif word == "jumble":
print("HINT: The name of the game.")
elif word == "easy":
print("HINT: Part of the Staples slogan.")
elif word == "difficult":
print("HINT: A synonym for hard.")
elif word == "answer":
print("HINT: Opposite of question.")
elif word == "xylophone":
print("HINT: An instrument.")
You are checking what word is, but I think that isn't the answer (it has been altered). Check correct instead:
if option == "yes":
hint = "yes"
if correct == "python":
print("HINT: This was used to make this game.")
elif correct == "jumble":
print("HINT: The name of the game.")
elif correct == "easy":
print("HINT: Part of the Staples slogan.")
elif correct == "difficult":
print("HINT: A synonym for hard.")
elif correct == "answer":
print("HINT: Opposite of question.")
elif correct == "xylophone":
print("HINT: An instrument.")
The problem lies here:
option = input("Would you like a hint? (yes/no)" + '\n').lower
option was set to lower, a string method. You need to call it:
option = input("Would you like a hint? (yes/no)" + '\n').lower()
^^
Now option will be a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With