0

[SOLVED] I want to add a number of variables depending on user input...

So, I've decided to create a questionnaire program to practically test my current knowledge on Boolean algebra. Doing little projects is probably going to help me solidify my knowledge the best. Here's the sibling part of the questionnaire - #Siblings questions siblings = input("How many siblings do you have? ") print('\n') siblings = int(siblings) sib_count = list(range(siblings)) while siblings > 0: sib_name = input("What is your sibling's name? ") siblings = siblings - 1 print (sib_name) print('\n') This works just fine, but I want to be able to have each answer to sib_name be its own variable, and I'm not quite sure how to do it. So, if I input that I have 3 siblings, I want to create sib_name1, sib_name2, sib_name3 so that I can have those variables stored for later output. I've made it through the first two sections, "Basic Concepts" and "Control Structures". In looking over through my notes and back through the lessons, I cannot figure out how to accomplish my goal. I'm figuring I have to integrate a list in order to do what I want, thus the sib_count variable I've created, but I cannot figure how to integrate it to do what I want. It may be that it hasn't been covered yet, and that I just need to have more patience to be able to do more. But in the case that I'm just not connecting the dots, I thought I'd ask here. Thanks in advance for anyone that gives their time to helping me solve this (probably rather simple) problem.

16th May 2020, 4:22 AM
Russell Allen
Russell Allen - avatar
5 Answers
+ 1
no_of_sib = int (input ("How many siblings do you have? \n ")) # you can directly use int above to typecast sib_list = [] #using list to store temporarily for i in range (no_of_sib): # taking name input name = input(" Enter name of your sibling : \t ") sib_list.append (name) # stored name in the list # printing from list for i in range (no_of_sib): print (sib_list [i]) print ("Writing Inside file...") # using "with open" to open file help you close file automatically !! with open (" siblings.txt", "a+") as sib_file: # using file to save it permanently on file for i in range (no_of_sib): # writing each name in file sib_file.write (f"{ sib_list [i] } \t") sib_file.write("\n") # or you can use one between them.
16th May 2020, 4:59 AM
Manish
Manish - avatar
+ 1
Thank you Manish, I'm going to test this out. The "with open" and .write are things I have not learned yet, so I may wait until that pops up in the lessons before I tinker with that part. Much appreciation for your input!
16th May 2020, 5:50 AM
Russell Allen
Russell Allen - avatar
+ 1
If you really want to create a variable for each sibling, you can try exec. for i in range(siblings) : sib_name=input(f"What's your number {i+1} sibling name?") exec(f"sib_name{i+1}={sib_name}") print(sib_name1) #first sibling name However, a better practice is to make a dictionary, and then assign key-value pairs as index-name pair e. g. sib_names={} for i in range(siblings) : sib_name=input(f"What's your number {i+1} sibling name?") sib_names[i+1] = sib_name print(sib_names[1]) #first sibling name
16th May 2020, 6:03 AM
æŽç«‹ćš
+ 1
For anyone interested, here's how I solved my dilemma and expanded upon the questionnaire, using Manish's tips - #Siblings questions no_of_sib = int(input("How many siblings do you have?\n ")) sib_list = [] print("Starting with the oldest,") for i in range(no_of_sib): sib_name = input("Enter your sibling's name: ") print("\n") sib_list.append(sib_name) #Lists for calculating sibling ages sib_year_list = [] sib_born_list = [] sib_bday_list = [] sib_age_list = [] for i in range(no_of_sib): sib_born = input('What year was ' + sib_list[i] + ' born? ') sib_born_list.append(sib_born) sib_year = int(sib_born) sib_year_list.append(sib_year) print('\n') for i in range(no_of_sib): sib_bday = input("Has " + sib_list[i] + " had their birthday this year? ") sib_bday_list.append(sib_bday) for i in range(no_of_sib): if sib_bday_list[i] == "no": sib_year_list[i] = int(sib_year_list[i] + 1) print('\n') #Age calculations for i in range(no_of_sib): sib_age = str(2020 - sib_year_list[i]) sib_age_list.append(sib_age) #Print ages of siblings for i in range(no_of_sib): print(sib_list[i] + ' is ' + sib_age_list[i] + ' years old.' '\n')
16th May 2020, 10:47 AM
Russell Allen
Russell Allen - avatar
0
æŽç«‹ćš - Thanks for your input! I know about libraries, but have yet to get to that part in the course, so I'm going to come back to your suggestions once I get to that point. Manish - I've been toying with your tips and suggestions and it's exactly what I wanted as far as executing things I've learned up to this point. I meant to say in my initial post that I wanted to make and utilize lists for this. I'm working on creating more questions using the sibling lists. Fun! Enjoying this learning process, so glad to have found an active and helpful community!
16th May 2020, 10:23 AM
Russell Allen
Russell Allen - avatar