我是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循环都会丢失?
提前感谢您的时间和帮助。
答案 0 :(得分:2)
问题在于,当您进入while guess != correct and guess != "":
循环时,word
已完全混乱。因此,在该循环中的所有if
和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: ")
答案 1 :(得分:1)
musical_coder是对的,解决了你的问题。您应该在代码中更改更多内容以使其更好:
不要将字符串用于提示变量,请使用boolean:
代替:
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.")
答案 2 :(得分:0)
这是让您的程序更简单的一种方法:
jumble = ''.join(random.sample(word, len(word)))