如何避免重复字符串?

时间:2015-09-19 14:10:03

标签: 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')

询问提示字符串时:

  

'对不起,你错了:('

重复。
它看起来像:

  

尝试猜测原始单词:提示
  运动游戏
  对不起,你错了:(

如果出现错误猜测,如何使这个字符串出现?

4 个答案:

答案 0 :(得分:3)

改变你的最后一次:

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

答案 1 :(得分:2)

在您的代码中,当玩家输入hint时,玩家会获得提示,但程序会针对'hint'字词测试correct字符串。当然,'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')

答案 2 :(得分:1)

让我们尝试解决一些问题:

if guess == hint: #situation if player asks a hint

应该是

elif guess == hint: #situation if player asks a hint

这对我来说也是错的

while guess != correct and guess != '': #situation if player is not correct
    print('Sorry, you`re wrong :(')
    guess = input('Try to guess the original word: ')

应该改为(缩进很重要):

    guess = input('Try to guess the original word: ')
if guess != correct and guess != '': #situation if player is not correct
    print('Sorry, you`re wrong :(')

我没有在完整的程序中尝试过这种更正。

答案 3 :(得分:1)

正确猜测和特殊输入"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语句将其显式添加为额外的大小写。