Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I fix my output on this simple name generating python project?

I am just trying to figure out what the issue is with my output on this simple name generating project in Python. The program work just fine and accepts all the inputs, but when it prints the result I get duplicates of certain names when I simply want a name printed for each int in my amount variable. Below is a copy of the code…

import random
import string

#Welcome to program

print("Welcome to the EC2 unique name generator")
print("")

depts = ['marketing', 'accounting', 'finops']
ec2list = []

#Department input and check
department = input("Please state your department (Marketing, Accounting, FinOps): ").lower()
#If check department unsuccessful print unauthorized message
if (department not in depts):
    print ("You are not authorized to use this EC2 name generator. ")
else: #if department check successful take amount
    print("")
    amount = int(input("Enter the amount of EC2 instances: "))
    #instance name creation. created instance, randomnum, randomchar, ec2name variables
    for instance in range(0, amount):
        randomnum = random.randint(100, 999)
        randomchar = ''.join(random.choices(string.ascii_letters, k=5))
        ec2name = "{}_{}{}{}".format(department, randomnum, randomchar, instance)
        ec2list.append(ec2name)
        #print formatted list
        print("\n".join(ec2list))

Below is an example of my output when I ask for the names of 5 instances. feel like this is a simple fix going over my head. Thanks in advance.
screenshot of print output

Welcome to the EC2 unique name generator

Please state your department (Marketing, Accounting, FinOps): finops

Enter the amount of EC2 instances: 5
finops_887zCseK0
finops_887zCseK0
finops_933rUCpF1
finops_887zCseK0
finops_933rUCpF1
finops_310vbvWB2
finops_887zCseK0
finops_933rUCpF1
finops_310vbvWB2
finops_288eQKTY3
finops_887zCseK0
finops_933rUCpF1
finops_310vbvWB2
finops_288eQKTY3
finops_924qOqWj4

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In each pass of the for loop, you add an item to ec2list, and then you print the whole cumulative list.

So in your first pass through the loop, you print ec2List[0]

In your second pass you print ec2List[0] again, and ec2List[1].

In the third pass you print ec2List[0] again, ec2List[1] again, and ec2List[2]

…and so on.

Instead of calling print("\n".join(ec2list)) within the for loop, you could call it just once after the loop is complete.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading