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完全にごちゃごちゃになっていることです。したがって、そのループ内のすべてのifandステートメントで、リスト内の 6 つの単語のいずれかと等しくelifなることはありません。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

プログラムを簡単にする方法の 1 つを次に示します。

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