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
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With