I need to create a list of random characters that are generated from random numbers. One list needs to be the letters generated from the random numbers, while the other list will get letters that are already in the first list. I have seen the import string method that uses the ascii, but I dont fully understand that. So far I have used the import random method and have been able to generate the random numbers but I feel my code is too long and that there should be an easier way. Each time a random number is generated the program should convert it to a letter and add to a list (called uniqueList) only if the number is not already present in the list. Stop adding the numbers once the list contains 15 characters.
here is a sample of the code I have so far:
uniqueList = []
excludedList = []
count = 0
while (len(uniqueList)<15):
randomNumber = random.randint(*random.choice([(65,90), (97,122)]))
print(randomNumber)
if(randomNumber == 65):
letter = 'A'
if(randomNumber == 66):
letter = 'B'
if(randomNumber == 67):
letter = 'C'
if(randomNumber == 68):
letter = 'D'
if(randomNumber == 69):
letter = 'E'
if(randomNumber == 70):
you can tell the code is long so i know there has to be a shorter way
Aucun commentaire:
Enregistrer un commentaire