每次我完成一轮,下一轮“混乱的单词”都是完全相同的,并且不会随机变化。不知道我做错了什么。我想要它,以便在你完成这个词后,你进入下一轮,显然它将是一个不同的词。如果那里有经验的程序员想帮助我,请做。 提前致谢
# word jumble game
# 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 = ("other", "jungle", "monday", "sword", "cat", "cheese", "snow", "england",
"planet", "bread")
# 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
rounds = 10
# create a jumbled version of the word
jumble = ""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print(
"""
Welcome to Word Jumble!
Unscramble the letters to make a word.
(Press the enter key at the prompt to quit.)
Score the highest points to win
There are 10 rounds
Press '?' for a hint, but using a hint will deduct
25 points of your total score
Good Luck!!!
""")
print ("The jumble:", jumble)
score = 100
# start the game
guess = ""
first = range(len(jumble))
rounds = 1
while True:
guess = input("Guess or '?': ").lower()
if guess == correct:
score += 50
rounds += 1
print ("That's it! You guessed it!\n your score is", score)
print("Round", rounds,"...")
print("The jumble is:", jumble)
elif guess == '?':
i = random.choice(first)
print ( correct[i], "is the", i+ 1, "letter.")
score -= 20
else:
print ("Sorry, thats not it. Try again.")
if rounds == 10:
print("That is the end of the game, your total score is", score, "Thank you for playing")
break
input("\n\nPress the enter key to exit.")
答案 0 :(得分:1)
因为您在while循环之外选择随机选择。将代码中的那部分移动到while循环的开头,每次游戏重新开始时都应该选择一个新的选择。
while word:
word = random.choice(WORDS)
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
您需要更改while循环的条件,因为单词在检查时仍然不存在。像while playing
和playing
之类的东西可以设置为假,因此你可以出于任何原因结束循环。
答案 1 :(得分:0)
您实际上并未生成新的jumble
。当玩家获胜时,会发生以下情况:
if guess == correct:
score += 50
rounds += 1
print ("That's it! You guessed it!\n your score is", score)
print("Round", rounds,"...")
print("The jumble is:", jumble) # still the same jumble
您可以将您的混音器封装到函数jumbled_word()
中,然后
jumble = jumbled_word()
print("The jumble is:", jumble) # now a new jumble
或者,尝试这样的结构(伪):
set up word_list
score = 0
while word_list:
remove a random word from word_list
jumble the word
while True:
guess
if guess == word:
score += 1
break
print score
答案 2 :(得分:0)
以下是我为使您的代码工作所做的事情:
raw_input
代替input
play_round
返回分数
play_game
循环10次,每次调用play_round
并为每轮创建一个新单词play_game
以下是play_game
的样子:
def play_game():
score = 100
rounds = 10
for round in range(rounds):
word = select_word()
jumble = jumbled(word)
score = play_round(word, jumble, round + 1, score)
print("That is the end of the game, your total score is {0}".format(score))
print("Thank you for playing")
raw_input("\n\nPress the enter key to exit.")