I am making a game similar to text twist on python and I was wondering how to prevent the user being able to input the same word twice. Here is what I have so far...

``import randomcorrect = 0incorrect = 0usedwords = []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 + 1if 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 + 1print 'Your final score was', correct, 'correct and', incorrect, 'wrong.'``

``````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.'
``````

This code can be modified as below:

``````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.'
``````

Changes:

• included "Continue" statement to skip the iteration when same word is repeated.
• incorrect = incorrect + 1 to increment the incorrect attempt when same word is repeated.

• Used if..elif..else instead of multiple if statements for better clarity and to ignore "!=" verification.

• Removed "iList itemf answer in usedwords:" verification in second if statement as it is already verified previously.

• If the word is not in usedwords or if the word is not in the expected word list, then obviously the word is incorrect, hence in else statement incorrect count is increased with a message.

Suggestion: Instead of using multiple OR in the elif statement, you can declare a list like, expectedwords = ['belle', 'bleep', 'pebble','beep','bell','peel','peep'] and use, elif answer in expectedwords: ....

Top