首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我不希望循环启动时

当我不希望循环启动时
EN

Stack Overflow用户
提问于 2013-10-21 22:34:55
回答 3查看 80关注 0票数 0

我正在研究while循环的格式,我仍然不确定(我是个初学者--请原谅我)如何解决这个问题。如能提供任何帮助,将不胜感激!当用户要求提示时,我不希望“抱歉,这不是它”消息弹出--但是它仍然坚持这样做。

代码语言:javascript
复制
# 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.")
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-21 22:45:09

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

代码语言:javascript
复制
    if hint_input == 'y':
        print(hint)
    guess = input("Your guess: ")
票数 2
EN

Stack Overflow用户

发布于 2013-10-21 22:45:28

您的代码工作正常,但是您需要在输入字段中输入一个包含"'的字符串。所以输入"jumble",而不仅仅是jumble"y",而不仅仅是y

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

票数 0
EN

Stack Overflow用户

发布于 2013-10-21 22:48:45

我复制了你的代码并运行了它。

我将输入更改为raw_input,这使输入更好。删除其他:打印后(提示),这将解决您的问题。最后,我在“您想要一个提示吗?”之后增加了额外的空间--使它更容易阅读。

代码语言:javascript
复制
# 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.")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19506035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档