
December 26th, 2012, 04:30 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 29
Time spent in forums: 11 h 39 sec
Reputation Power: 0
|
|
|
While loop asking for two inputs
So I am trying to re write a jumble word program to add hints and also reward players for not viewing hints.
I have been trying to make it to where the only inputs accepted when the program asks if you want a hint is "Y" or "N". I am using a nested while loop and the program repeats the question if they want a hint twice. I am pretty sure its how I have it set-up but I am not sure how to make it ask only once. Here is the code.
Code:
# Word Jumble
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word
import random
WORDS = ("python",
"jumble",
"easy",
"difficult",
"answer",
"xylophone")
HINTS = ("\nDynamic programming language.",
"\nA way of mixing things up.",
"\nVery simple.",
"\nNot very simple.",
"\nNeeded after a question.",
"\nA weird word starting with X.")
word = random.choice(WORDS)
correct = word
jumble = ""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print("""
Welcome to Word Jumble!
Unscramble the letters to make a word.
(Press the ENTER key at prompt to quit.)
""")
print("The jumble is:",jumble)
guess = input("\nYour guess: ")
while guess != correct and guess != "":
print("\nSorry that's not it.")
hint = input("\nWould you like a hint? Y/N: ")
hint_views = 1
while hint != "Y" or "N":
print("\nPlease choose either 'Y' or 'N'.")
hint = input("\nWould you like a hint? Y/N: ")
if hint == "Y" and correct == WORDS[0]:
print(HINTS[0])
hint_views += 1
break
if hint == "Y" and correct == WORDS[1]:
print(HINTS[1])
hint_views += 1
break
if hint == "Y" and correct == WORDS[2]:
print(HINTS[2])
hint_views += 1
break
if hint == "Y" and correct == WORDS[3]:
print(HINTS[3])
hint_views += 1
break
if hint == "Y" and correct == WORDS[4]:
print(HINTS[4])
hint_views += 1
break
if hint == "Y" and correct == WORDS[5]:
print(HINTS[5])
hint_views += 1
break
if hint == "N":
print("\nOK, maybe next time!")
break
guess = input("\nYour guess: ")
if guess == correct and hint_views >= 2:
print("\nThat's it! Hmmm, you viewed the hints",hint_views,"times.")
print("You are basically dumb, but congratulations!")
if guess == correct and hint_views <= 1:
print("\nThat's it! Wow, your pretty smart. You guess it without")
print("looking at any hints!")
input("\nPress the ENTER key to exit.")
Everything works ok, just when the program asks if you want a hint, it asks twice.
|