Skip to content
Advertisement

How to change the name of the dictionary while iterating over it?

How can I change the name of the dictionary while, iterating it over a while-loop(or any other)… so that it saves the input information in a dictionary with different name every single time?

This would save the information(as a dictionary) in different names(like biodata1, biodata2, etc…) so that processing those informations become easy!!

    ans = input("Can we start?[(Y)es or (N)o]: ")
    ans.lower()
    while ans == "y":

        # Name
        name = input("Name: ")
        # -----------------------
        # Age
        age = int(input("Age: "))
        if age >= 18:
            soc_stat = "Adult"
        else:
            soc_stat = "Minor"
        # -----------------------
        # DOB
        dob = input('''
        Enter your D.O.B. in D, M, Y format
        [must include comma after each item, 
        but don't finish with comma]: ''')
        # ------------------------------
        # E-Mail
        email = input("Your E-Mail id: ")
        # ------------------------
        # Gender
        gender = input("(M)ale or (F)emale: ")
        gender.lower()
        if gender == "m":
            gender = "Male"
        elif gender == "f":
            gender = "Female"
        # -----------------------
        # Criminal Records
        crim = input("Do you have any criminal records(your input will be cross-checked)[(Y)es or (N)o]: ")
        crim.lower()
        if crim == "y":
            has_criminal_records = True
        elif crim == "n":
            has_criminal_records = False

        biodata = {
            "Name": name,
            "Age": age,
            "DOB": (dob[0], dob[1], dob[2]),
            "Social status": soc_stat,
            "EMail": email,
            "Gender": gender,
            "Criminal records": has_criminal_records,
        }
        break
        

Advertisement

Answer

What I would advice would be instead of while loop define function that will perform codes inside your loop and after append your dictionary to a list.

Your code should be like this:

biodata_list= []

def biodata_input():
    ans = input("Can we start?[(Y)es or (N)o]: ")
    ans.lower()

    if ans == "y":
        # Name
        name = input("Name: ")
        # -----------------------
        # Age
        age = int(input("Age: "))
        if age >= 18:
            soc_stat = "Adult"
        else:
            soc_stat = "Minor"
        # -----------------------
        # DOB
        dob = input('''
                Enter your D.O.B. in D, M, Y format
                [must include comma after each item, 
                but don't finish with comma]: ''')
        # ------------------------------
        # E-Mail
        email = input("Your E-Mail id: ")
        # ------------------------
        # Gender
        gender = input("(M)ale or (F)emale: ")
        gender.lower()
        if gender == "m":
            gender = "Male"
        elif gender == "f":
            gender = "Female"
        # -----------------------
        # Criminal Records
        crim = input("Do you have any criminal records(your input will be cross-checked)[(Y)es or (N)o]: ")
        crim.lower()
        if crim == "y":
            has_criminal_records = True
        elif crim == "n":
            has_criminal_records = False

        biodata_dict = {
            "Name": name,
            "Age": age,
            "DOB": (dob[0], dob[1], dob[2]),
            "Social status": soc_stat,
            "EMail": email,
            "Gender": gender,
            "Criminal records": has_criminal_records,
        }
        biodata_list.append(biodata_dict)

biodata_input()
print(biodata_list)

With this way you have to call biodata_input() function each time you want user to input data. You can get this into while loop if you are going to ask endlessly for input.

One think I noticed, your age input is written like below;

age = int(input("Age: "))

So if user types a character by mistake this will cause Value Error and script will exit. Instead after age input, you can check for type, and if its not an integer you can ask for input again.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement