Python Strings Medical Insurance Project help

Welcome to the Get Help category!

I am attempting the extra work (task #22) in the Python Strings Medical Insurance Project. The task is this:

• Write a for loop that outputs a string for each individual in the following format:
``````Marina is 27 year olds with a BMI of 31.1 and an insurance cost of \$7010.0.Markus is 30 years old with a BMI of 22.4 and an insurance cost of \$4050.0......
``````

We already have separated out all of the relevant information into separate lists of: names, ages, bmis, insurance_costs.

My attempted code looks like this:

for i in names:
i = 0
print(names[i] + β is β + ages[i] + β years old with a BMI of β + bmis[i] + β and an insurance cost of β + insurance_costs[i] + β.β)
i += 1

The output prints the same information ten times, all of it the information for indices[0]. Why is it not moving to the next indices when it prints? Any help would be great!

This is where you can ask questions about your code. Some important things to remember when posting in this category

• Learn how to ask a good question and get a good answer!
• Remember to include a link to the exercise you need help with!
• Once you understand a new concept, come back and try to help someone else!

Take a look at your `for` loop. What is the first thing that happens on each iteration of the loop?

I was having nightmare with this as well but I got it to work this way.

for i in range(len(names)):
name = names[i]
age = ages[i]
bmi = bmis[i]
insurance_cost = insurance_costs[i]
print(str(name)+ β is β +str(age)+ β year olds with a BMI of β + str(bmi)+ β and an insurance cost of '+ str(insurance_cost))

I am sure there is another way to use medical_record_clean list rather than separate lists on which we ended up on at the end of the project as well but I couldnβt get that to work.

1 Like

Not really sure what youβre trying to get me to see/understand with my first line el_cocodrilo, but apparently adding the range(len(names)) while deleting the counter did the trick and got each line to print correctly.

``````for i in range(len(names)):

print(names[i] + ' is ' + ages[i] + ' years old with a BMI of ' + bmis[i] + ' and an insurance cost of ' + insurance_costs[i] + '.')
``````

f
This code does the same job without having to do the name = names[i] part

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.