0

我是 Python 的初学者,我坚持练习。书中有一个游戏叫Word Jumple。以下是我需要做的:改进“Word Jumble”,让每个单词都与提示配对。如果他或她被卡住,玩家应该能够看到提示。添加一个评分系统,奖励在不询问提示的情况下解决混乱的玩家。

这就是我所做的:

# 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)

# create a variable to use later to see if the guess is correct
correct = word

# create a jumbled version of the word
jumble =""
hint = "False"

while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]

# 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 != "":
    if guess == "hint" and word == "python":
        hint = "True"
        print("It's a snake")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "jumble":
        hint = "True"
        print("It's a game")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "easy":
        hint = "True"
        print("It's type of difficulty")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "difficulty":
        hint = "True"
        print("It's type of difficulty")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "answer":
        hint = "True"
        print("It's the opposite of question")
        guess = input("Your guess: ")
    elif guess == "hint" and word == "xylophone":
        hint = "True"
        print("Don't know WTF is that")
        guess = input("Your guess: ")
    else:
        print("Sorry, that's not it.")
        guess = input("Your guess: ")

if guess == correct:
    print("That's it! You guessed it!\n")

if hint == "False":
    print("Great! You did it without a hint")
else:
    print("Dat hint, man")

print("Thanks for playing.")
input("\n\nPress the enter key to exit.")

结果我有这个:

Welcome to Word Jumble!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)

The jumble is: jbelum

Your guess: hint
Sorry, that's not it.
Your guess: jumble
That's it! You guessed it!

Great! You did it without a hint
Thanks for playing.


Press the enter key to exit.

为什么当输入为“提示”并直接进入 else 子句时,while 循环全部丢失?

提前感谢您的时间和帮助。

4

3 回答 3

2

问题是当你进入while guess != correct and guess != "":循环时,word已经完全混乱了。因此,在该循环中的所有ifandelif语句中,您将永远不会word等于列表中的六个单词之一。

correct要解决此问题,请word在这些语句中替换:

if guess == "hint" and correct == "python":
    hint = "True"
    print("It's a snake")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "jumble":
    hint = "True"
    print("It's a game")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "easy":
    hint = "True"
    print("It's type of difficulty")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "difficulty":
    hint = "True"
    print("It's type of difficulty")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "answer":
    hint = "True"
    print("It's the opposite of question")
    guess = input("Your guess: ")
elif guess == "hint" and correct == "xylophone":
    hint = "True"
    print("Don't know WTF is that")
    guess = input("Your guess: ")
else:
    print("Sorry, that's not it.")
    guess = input("Your guess: ")
于 2013-10-20T19:08:53.360 回答
1

music_coder 是对的,解决了你的问题。您应该在代码中更改更多内容以使其更好:

不要使用字符串作为提示变量,使用布尔值:

反而:

hint = "False"
hint = "True"

利用:

hint = False
hint = True

您也可以稍微重写一下while循环以拥有

 guess = input("Your guess: ")

只有一次,而不是每一个如果。

此外,稍微更改结果打印,因为这样即使您输入空字符串并退出游戏,它也会打印提示信息。

所以它可能看起来像这样:

print("The jumble is:", jumble)
guess = "dummy"
while guess != correct and guess != "":
    guess = raw_input("Your guess: ")
    if guess == "hint" and correct == "python":
        hint = True
        print("It's a snake")
    elif guess == "hint" and correct == "jumble":
        hint = True
        print("It's a game")
    elif guess == "hint" and correct == "easy":
        hint = True
        print("It's type of difficulty")
    elif guess == "hint" and correct == "difficulty":
        hint = True
        print("It's type of difficulty")
    elif guess == "hint" and correct == "answer":
        hint = True
        print("It's the opposite of question")
    elif guess == "hint" and correct == "xylophone":
        hint = True
        print("Don't know WTF is that")
    else:
        print("Sorry, that's not it.")

if guess == correct:
    print("That's it! You guessed it!\n")

    if not hint:
        print("Great! You did it without a hint")
    else:
        print("Dat hint, man")

print("Thanks for playing.")
input("\n\nPress the enter key to exit.")
于 2013-10-20T19:32:06.193 回答
0

这是使您的程序更简单的一种方法:

jumble = ''.join(random.sample(word, len(word)))
于 2013-10-20T19:05:12.730 回答