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
CodePudding user response:
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.
CodePudding user response:
The dictionary doesn't have a name, per se. There is a refence to a dictionary in a variable named biodata
. On each iteration, the variable contains a reference to a different dictionary with different data from the user.
You may want to attach that dictionary object to some other data structure. Perhaps a list:
all_biodata = [];
...
while ...:
...
all_biodata.append(biodata)
Once that loop has ended, you'll have a list of all biodata entries.
Note that your loop appears to continue infinitely. You may need to change ans
to cause it to exit at some point, perhaps by prompting the user for an indication of whether to continue looping.