I managed to create a simple random string generator that prints a random string of letters, x number of times, with each iteration in the list having a random length between 3-10 characters. I'm new to Python, and am still learning the very basics, so bear with me. My program works as intended, but I am pretty sure my code can be optimized. I'm really looking to learn best practices and expand my knowledge. This is my first post. Apologies if I'm in the wrong place.
import random
class Randomize:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
choice1 = random.choice(alphabet)
choice2 = random.choice(alphabet)
choice3 = random.choice(alphabet)
choice4 = random.choice(alphabet)
choice5 = random.choice(alphabet)
choice6 = random.choice(alphabet)
choice7 = random.choice(alphabet)
choice8 = random.choice(alphabet)
choice9 = random.choice(alphabet)
choice10 = random.choice(alphabet)
three_letters = choice1 + choice2 + choice3
four_letters = choice1 + choice2 + choice3 + choice4
five_letters = choice1 + choice2 + choice3 + choice4 + choice5
six_letters = choice1 + choice2 + choice3 + choice4 + choice5 + choice6
seven_letters = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7
eight_letters = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8
nine_letters = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9
ten_letters = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10
word_length = [three_letters, four_letters, five_letters, six_letters, seven_letters, eight_letters, nine_letters, ten_letters]
word = random.choice(word_length)
def gen(self):
for i in range(10):
print (Randomize.word)
Randomize.choice1 = random.choice(Randomize.alphabet)
Randomize.choice2 = random.choice(Randomize.alphabet)
Randomize.choice3 = random.choice(Randomize.alphabet)
Randomize.choice4 = random.choice(Randomize.alphabet)
Randomize.choice5 = random.choice(Randomize.alphabet)
Randomize.choice6 = random.choice(Randomize.alphabet)
Randomize.choice7 = random.choice(Randomize.alphabet)
Randomize.choice8 = random.choice(Randomize.alphabet)
Randomize.choice9 = random.choice(Randomize.alphabet)
Randomize.choice10 = random.choice(Randomize.alphabet)
Randomize.three_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3
Randomize.four_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4
Randomize.five_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5
Randomize.six_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5 + Randomize.choice6
Randomize.seven_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5 + Randomize.choice6 + Randomize.choice7
Randomize.eight_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5 + Randomize.choice6 + Randomize.choice7 + Randomize.choice8
Randomize.nine_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5 + Randomize.choice6 + Randomize.choice7 + Randomize.choice8 + Randomize.choice9
Randomize.ten_letters = Randomize.choice1 + Randomize.choice2 + Randomize.choice3 + Randomize.choice4 + Randomize.choice5 + Randomize.choice6 + Randomize.choice7 + Randomize.choice8 + Randomize.choice9 + Randomize.choice10
Randomize.word_length = [Randomize.three_letters, Randomize.four_letters, Randomize.five_letters, Randomize.six_letters, Randomize.seven_letters, Randomize.eight_letters, Randomize.nine_letters, Randomize.ten_letters]
Randomize.word = random.choice(Randomize.word_length)
name = Randomize()
name.gen()
print('Generated 10 names.')
Aucun commentaire:
Enregistrer un commentaire