BAAAAALI
BAAAAALI

Reputation: 17

How to check for duplicates in a string but not replace them?

Sample user input

letters = input("Please input the scrambled letters in order: ")

Now we all know that there are only 26 letter in english and none of them repeat. So how do I make sure that whatever the user inputs doesn't repeat(don't need to replace)? I need to write an if statement with that algorithm.

if letters == nothing_duplicate:
    do something

Upvotes: 1

Views: 86

Answers (2)

jterrace
jterrace

Reputation: 67103

If you want to check for duplicates and verify that they have input every letter:

import string
if set(letters.lower()) == set(string.lowercase):
  # do something

To actually get the list of letters that are missing, you could do something like this:

>>> set(string.lowercase).difference('abcdefghijklmnopqrst')
set(['u', 'w', 'v', 'y', 'x', 'z'])

Upvotes: 0

dansalmo
dansalmo

Reputation: 11696

if len(letters) == len(set(letters)):
    do something

Upvotes: 3

Related Questions