阻止用户两次输入相同的输入

时间:2016-03-10 05:15:04

标签: python

我正在制作类似于python上的文字扭曲的游戏,我想知道如何防止用户能够输入两次相同的单词。这是我到目前为止所拥有的......

import random
correct = 0
incorrect = 0
usedwords = []
print 'Welcome to text twist, you have 14 guesses to get 7 words made up of 4, 5 or 6 letters. Good Luck!'

for i in range(14):
    print "Your letters are 'E' 'P' 'L' 'B' 'E' 'B', what is your guess?"
    answer = raw_input()
    if answer in usedwords:
        print "Sorry, you've already used this word"
    if answer == 'belle' or answer == 'bleep' or answer == 'pebble' or answer == 'beep' or answer == 'bell' or answer == 'peel' or answer == 'peep':
        if answer in usedwords:
            print 'Nice that was one of the words!'
        usedwords.append(answer)
        correct = correct + 1
    if answer != 'belle' and answer != 'bleep' and answer != 'pebble' and answer != 'beep' and answer != 'bell' and answer != 'peel' and answer != 'peep':
        print 'Sorry, that was not one of the words.'
        incorrect = incorrect + 1


print 'Your final score was', correct, 'correct and', incorrect, 'wrong.'

2 个答案:

答案 0 :(得分:1)

import random
correct = 0
incorrect = 0
usedwords = []
print 'Welcome to text twist, you have 14 guesses to get 7 words made up of 4, 5 or 6 letters. Good Luck!'

for i in range(14):
    print "Your letters are 'E' 'P' 'L' 'B' 'E' 'B', what is your guess?"
    answer = raw_input()
    if answer in usedwords:
        print "Sorry, you've already used this word"
    else: #Newly added
        usedwords.append(answer) #We have to add the input words to the list if they are new
        if answer == 'belle' or answer == 'bleep' or answer == 'pebble' or answer == 'beep' or answer == 'bell' or answer == 'peel' or answer == 'peep':
            if answer in usedwords:
                print 'Nice that was one of the words!'
            usedwords.append(answer)
            correct = correct + 1
        if answer != 'belle' and answer != 'bleep' and answer != 'pebble' and answer != 'beep' and answer != 'bell' and answer != 'peel' and answer != 'peep':
            print 'Sorry, that was not one of the words.'
            incorrect = incorrect + 1


print 'Your final score was', correct, 'correct and', incorrect, 'wrong.'

答案 1 :(得分:1)

此代码可以修改如下:

import random
correct = 0
incorrect = 0
usedwords = []
print 'Welcome to text twist, you have 14 guesses to get 7 words made up of 4, 5 or 6 letters. Good Luck!'
for i in range(14):
    print "Your letters are 'E' 'P' 'L' 'B' 'E' 'B', what is your guess?"
    answer = raw_input()
    if answer in usedwords:
        print "Sorry, you've already used this word"
        incorrect = incorrect + 1
        continue
    elif answer == 'belle' or answer == 'bleep' or answer == 'pebble' or answer == 'beep' or answer == 'bell' or answer == 'peel' or answer == 'peep':
        print 'Nice that was one of the words!'
        usedwords.append(answer)
        correct = correct + 1
    else:
        print 'Sorry, that was not one of the words.'
        incorrect = incorrect + 1


print 'Your final score was', correct, 'correct and', incorrect, 'wrong.'

<强>的变化:

  • 包含“Continue”语句,以便在同一个单词时跳过迭代     重复。
  • 不正确=错误+ 1,在重复同一个字时增加不正确的尝试。

  • 使用if..elif..else而不是多个if语句以更清晰,并忽略“!=”验证。

  • 删除了“使用过的词中的iList itemf answer:”在第二个if语句中进行验证,因为之前已经验证过。

  • 如果单词不在用词中,或者单词不在预期单词列表中,那么这个单词显然不正确,因此在else语句中,错误计数会随着消息而增加。

<强>建议: 您可以声明一个列表,而不是在elif语句中使用多个OR,     expectedwords = ['belle','bleep','pebble','beep','bell','peel','peep'] 并使用,     预期词中的elif答案:         ....

相关问题