0

我正在检查 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.")
4

3 回答 3

2

删除 else: 因为你总是希望从用户那里得到一个新的猜测,不管他们是否收到了提示。

    if hint_input == 'y':
        print(hint)
    guess = input("Your guess: ")
于 2013-10-21T22:45:09.370 回答
0

您的代码工作正常 - 但您应该在输入字段中输入包含"or的字符串。'所以输入"jumble"and not just jumbleor "y"and not just y

(好吧,那里有一些奇怪的逻辑,例如在给出提示后它再次询问您是否需要提示 - 只需删除else以摆脱这种行为)但至少它有效......)

于 2013-10-21T22:45:28.473 回答
0

我已经复制了你的代码并运行了它。
我将输入更改为 raw_input,这使输入变得更好。删除 else: 在 print(hint) 之后,这应该可以解决您的问题。最后,我在“您需要提示吗?”之后添加了额外的空格 - 使其更易于阅读。

# 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.")
于 2013-10-21T22:48:45.020 回答