我的代码是:
import random
WORDS = ('python', 'football', 'facebook', 'photo') #list of words that will be riddled
word = random.choice(WORDS)
correct = word
jumble = ''
hint = 'hint'
score = 0
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):] #creating jumble of correct words
print('Welcome to the game "Anagrams"')
print('Here`s your anagram:', jumble) #Welcoming and giving a jumble to a player
guess = input('\nTry to guess the original word: ')
if guess == correct:
score += 5
print('You won! Congratulations!') #end of game in case of right answer
if guess == hint: #situation if player asks a hint
if correct == WORDS[0]:
print('snake')
elif correct == WORDS[1]:
print('sport game')
elif correct == WORDS[2]:
print('social network')
elif correct == WORDS[3]:
print('picture of something')
score += 1
while guess != correct and guess != '': #situation if player is not correct
print('Sorry, you`re wrong :(')
guess = input('Try to guess the original word: ')
print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')
询问提示字符串时:
'Sorry, you`re wrong :('
重复。
它看起来像:
Try to guess the original word: hint
sport game
Sorry, you`re wrong :(
如何使这个字符串只在猜错的情况下出现?你知道吗
在您的代码中,当播放机键入
hint
时,播放机会得到一个提示,但是程序会针对correct
单词测试'hint'
字符串。当然,'hint'
不是正确的答案,所以你的程序告诉他们这是错误的。你知道吗只是为了好玩,我对你的代码进行了一些优化,并改进了评分逻辑。:)
字母混乱
for
循环非常聪明,但是有一种更有效的方法,使用random.shuffle
函数。此函数用于就地无序排列列表。因此,我们需要将所选单词转换成一个列表,将其洗牌,然后将列表连接回一个字符串。你知道吗我还替换了你的逻辑提示。不必做一大堆
if
测试来查看哪个提示与当前单词匹配,只需将每个单词及其关联的提示存储为元组就简单多了。你知道吗正确猜测和特殊输入的特殊逻辑
"hint"
仅在第一次猜测时运行一次。不正确值的循环总是在那之后运行。我想你应该把所有的逻辑都放到循环中:我省略了代码在空输入时退出循环的情况。如果您想这样做,您应该使用
break
语句将其显式添加为一个额外的case。你知道吗上次更改为:
相关问题 更多 >
编程相关推荐