虽然循环启动时我不想要它

时间:2013-10-21 22:34:55

标签: python while-loop

我正在讨论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.")

3 个答案:

答案 0 :(得分:2)

删除else:因为您总是希望得到用户的新猜测,无论他们是否收到提示。

    if hint_input == 'y':
        print(hint)
    guess = input("Your guess: ")

答案 1 :(得分:0)

您的代码运行正常 - 但您需要在输入字段中输入包含"'的字符串。因此,请输入"jumble"而不只是jumble"y",而不只是y

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

答案 2 :(得分:0)

我已经复制了你的代码并运行了它 我将输入更改为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.")