我正在研究while循环的格式,我仍然不确定(我是个初学者--请原谅我)如何解决这个问题。如能提供任何帮助,将不胜感激!当用户要求提示时,我不希望“抱歉,这不是它”消息弹出--但是它仍然坚持这样做。
# Word Jumble
#
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word
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)
hint = ''
if word == 'python':
hint = 'snake'
if word == 'jumble':
hint = 'jumble'
if word == 'easy':
hint = 'opposite of hard'
if word == 'difficult':
hint = 'opposite of easy'
if word == 'answer':
hint = 'question'
if word == 'xylophone':
hint = 'dingding'
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
count = 0
# 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("\nYour guess: ")
while guess != correct and guess != "":
print("Sorry, that's not it.")
count += 1
hint_input = input('would you like a hint')
if hint_input == 'y':
print(hint)
else:
guess = input("Your guess: ")
if guess == correct:
print("That's it! You guessed it!\n")
print("Thanks for playing.")
input("\n\nPress the enter key to exit.")发布于 2013-10-21 22:45:09
删除of :因为您总是希望从用户那里得到一个新的猜测,而不管他们是否收到了提示。
if hint_input == 'y':
print(hint)
guess = input("Your guess: ")发布于 2013-10-21 22:45:28
您的代码工作正常,但是您需要在输入字段中输入一个包含"或'的字符串。所以输入"jumble",而不仅仅是jumble或"y",而不仅仅是y。
(嗯,里面有一些奇怪的逻辑,例如,在给出提示之后,它再次询问您是否需要一个提示- jus删除else以消除这种行为),但至少它有效.
发布于 2013-10-21 22:48:45
我复制了你的代码并运行了它。
我将输入更改为raw_input,这使输入更好。删除其他:打印后(提示),这将解决您的问题。最后,我在“您想要一个提示吗?”之后增加了额外的空间--使它更容易阅读。
# Word Jumble
#
# The computer picks a random word and then "jumbles" it
# The player has to guess the original word
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)
hint = ''
if word == 'python':
hint = 'snake'
if word == 'jumble':
hint = 'jumble'
if word == 'easy':
hint = 'opposite of hard'
if word == 'difficult':
hint = 'opposite of easy'
if word == 'answer':
hint = 'question'
if word == 'xylophone':
hint = 'dingding'
# create a variable to use later to see if the guess is correct
correct = word
# create a jumbled version of the word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
count = 0
# 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 = raw_input("\nYour guess: ")
while guess != correct and guess != "":
print("Sorry, that's not it.")
count += 1
hint_input = raw_input('would you like a hint? ')
if hint_input == 'y':
print(hint)
guess = raw_input("Your guess: ")
if guess == correct:
print("That's it! You guessed it!\n")
print("Thanks for playing.")
raw_input("\n\nPress the enter key to exit.")https://stackoverflow.com/questions/19506035
复制相似问题