Justin
Justin

Reputation: 41

How to stop same letter appearing twice in list?

im trying to write a progam where you enter either a vowel or a consonant 8 times and the list of letters you have chosen is then shown. Is there a way to program it so that the same letter cannot come up twice, e.g if you select vowel and get the letter a, then the letter a cannot be randomly chosen again? This is the program so far:

lt = 0
letters = []
while lt<8:
    letter = raw_input("Please enter v for a Vowel or c for a Consonant: ")
    if letter == "c":
        letters.append(random.choice(consonant)),
        lt = lt + 1
    elif letter == "v":
        letters.append(random.choice(vowel)),
        lt = lt + 1
    else:
        print("Please enter only v or c")

print ("letters:")
print letters

Upvotes: 4

Views: 855

Answers (4)

eumiro
eumiro

Reputation: 212905

Create a list of all consonnants and of all vowels, shuffle them randomly and then take one element at a time:

import random

con = list('bcdfghjklmnpqrstvwxyz') # in some languages "y" is a vowel
vow = list('aeiou')
random.shuffle(con)
random.shuffle(vow)
# con is now: ['p', 'c', 'j', 'b', 'q', 'm', 'r', 'n', 'y', 'w', 'f', 'x', 't', 'g', 'l', 'd', 'k', 'h', 'z', 'v', 's'] or similar
# vow is now: ['e', 'u', 'i', 'a', 'o'] or similar

letters = []
while len(letters) < 8:
    letter = raw_input("Please enter v for a Vowel or c for a Consonant: ")
    if letter == "c":
        if con:
            letters.append(con.pop())
        else:
            print("No more consonnants left")
    elif letter == "v":
        if vow:
            letters.append(vow.pop())
        else:
            print("No more vowels left")
    else:
        print("Please enter only v or c")

Upvotes: 8

pranshus
pranshus

Reputation: 187

you can do this

lt = {}
while len(lt.keys()) < 8:
    letter = raw_input("Please enter v for a Vowel or c for a Consonant: ")
    added == false
    while added != true:
        if letter == "c":
            toAdd = random.choice(consonant)
        elif letter == "v":
            toAdd = random.choice(vowel)
        else:
            print("Please enter only v or c")
        if not lt.has_key(toAdd): 
            lt[toAdd] = 1
            added = false
    letters = lt.keys()

Upvotes: 2

APerson
APerson

Reputation: 8422

It probably would be a good idea to check if the list already contained the consonant or vowel right before you added it to the list. For example, here would be the while loop with such checks:

while lt<8:
    letter = raw_input("Please enter v for a Vowel or c for a Consonant: ")
if letter == "c":
    c = random.choice(consonant)
    while c not in letters:
        c = random.choice(consonant)
    letters.append(random.choice(consonant))
    lt = lt + 1
elif letter == "v":
    v = random.choice(vowel)
    while v not in letters:
        v = random.choice(vowel)
    letters.append(random.choice(vowel))
    lt = lt + 1
else:
    print("Please enter only v or c")

The inner while loops are so that if the random choice is already in the list, the program chooses another letter.

Upvotes: 1

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11048

change letters from list to set:

letters = set()
>>> letters.add('x')
>>> letters.add('x')
>>> letters
set(['x'])

for reference: Python sets

edit: just noticed you were asking for something different than how does a set work, eumiro's answer is what you're looking for. if you wan to keep this for reference, it's fine, otherwise i'll delete my answer

Upvotes: 6

Related Questions