Home > Blockchain >  Storing data from input-variable in a list
Storing data from input-variable in a list

Time:01-04

I am currently studying a subject in programming using Python and have an upcoming exam. Code with 170 lines has been uploaded with a disposition for my oral examination but I want to improve some parts.

In the first part I had difficulties storing input, which is height, weight and birthyear from a ''respondent'' using my program, in a list. The inputs will later be used to calculate the BMI and give some health tips.

I have tried finding help on many sites and using the suggestions. Maybe something is wrong with my code?

# Input from respondent
Name = input('Type in your name : ')
Birthyear = input('Type in your birth year : ')
Height = input('Type in your height in inches: ')
Weight = input('Type in your weight in pounds : ')

Informations = (f'\n\nName\t\t: {Name.capitalize()}\n'
                 f'Birthyear\t: {Birthyear.capitalize()}\n'
                 f'Height\t\t: {Height.capitalize()}\n'
                 f'Weight\t\t: {Weight.capitalize()}\n')

print(Informations)

After this I would love for the input to be stored in a list containing fictional info on height, weight and birth year like the following:

# Create list with heights(BMItest_H), weights(BMItest_W) and birth year(BMItest_B) from 
fictional BMI test persons
BMItest_H = [70, 80, 78, 78, 75, 74, 77, 76]
BMItest_V = [176, 204, 199, 200, 187, 181, 180, 182]
BMItest_B = [1994, 1992, 1992, 1990, 1989, 1991, 1988, 1990]

CodePudding user response:

You can simply loop and append to a list to get multiple "respondents."

So for an example with 10 respondents:

Birthyear_list = []
Height_list = []
Weight_list = []
name_list = []
respondents = 10
for n in range(respondents):
    # Input from respondent
    Name = input('Type in your name : ')
    Birthyear = input('Type in your birth year : ')
    Height = input('Type in your height in inches: ')
    Weight = input('Type in your weight in pounds : ')

    Informations = (f'\n\nName\t\t: {Name.capitalize()}\n'
                    f'Birthyear\t: {Birthyear.capitalize()}\n'
                    f'Height\t\t: {Height.capitalize()}\n'
                    f'Weight\t\t: {Weight.capitalize()}\n')
    Birthyear_list.append(Birthyear)
    Height_list.append(Height)
    Weight_list.append(Weight)
    name_list.append(Name)

    print(Informations)
print(Birthyear_list)
print(Height_list)
print(Weight_list)
print(name_list)
  •  Tags:  
  • Related