如何避免重复字符串?

2024-09-29 23:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:

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 :(

如何使这个字符串只在猜错的情况下出现?你知道吗


Tags: ofthetoyougameifpositionword
3条回答

在您的代码中,当播放机键入hint时,播放机会得到一个提示,但是程序会针对correct单词测试'hint'字符串。当然,'hint'不是正确的答案,所以你的程序告诉他们这是错误的。你知道吗

只是为了好玩,我对你的代码进行了一些优化,并改进了评分逻辑。:)

字母混乱for循环非常聪明,但是有一种更有效的方法,使用random.shuffle函数。此函数用于就地无序排列列表。因此,我们需要将所选单词转换成一个列表,将其洗牌,然后将列表连接回一个字符串。你知道吗

我还替换了你的逻辑提示。不必做一大堆if测试来查看哪个提示与当前单词匹配,只需将每个单词及其关联的提示存储为元组就简单多了。你知道吗

import random

#Words that will be riddled, and their hints
all_words = (
    ('python', 'snake'),
    ('football', 'sport game'),
    ('facebook', 'social network'),
    ('photo', 'picture of something'),
)

#Randomly choose a word
word, hint = random.choice(all_words)

#Jumble up the letters of word
jumble = list(word)
random.shuffle(jumble)
jumble = ''.join(jumble)    

print('Welcome to the game "Anagrams"\n')
print('You may ask for a hint by typing hint at the prompt')
print('Wrong guesses cost 2 points, hints cost 1 point\n')

print("Here's your anagram:", jumble)

score = 0
while True:
    guess = input('\nTry to guess the original word: ')
    if guess == word:
        score += 5
        print('You won! Congratulations!')
        break

    if guess == 'hint':
        #Deduct a point for asking for a hint
        score -= 1
        print(hint)
        continue

    #Deduct 2 points for a wrong word
    score -= 2
    print('Sorry, you`re wrong :(')

print('Thank you for participating in game.')
print('Your score is', score)
input('\nPress Enter to end')

正确猜测和特殊输入的特殊逻辑"hint"仅在第一次猜测时运行一次。不正确值的循环总是在那之后运行。我想你应该把所有的逻辑都放到循环中:

while True:  # loop forever until a break statement is reached
    guess = input('\nTry to guess the original word: ')
    if guess == correct:
        score += 5
        print('You won! Congratulations!')
        break # stop looping
    if guess == hint: # special case, asking for 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
    else: #situation if player is not correct, and not askng for a hint
        print('Sorry, you`re wrong :(')

我省略了代码在空输入时退出循环的情况。如果您想这样做,您应该使用break语句将其显式添加为一个额外的case。你知道吗

上次更改为:

while guess != correct and guess != '':
    guess = input("Sorry, you`re wrong:( ")

相关问题 更多 >

    热门问题