Python Strings: Medical Insurance Project Extra Task

Hello guys, here I have a question about the extra task in the string medical insurance project:
Here I have 4 lists:

Names: [‘MARINA ALLISON’, ‘MARKUS VALDEZ’, ‘CONNIE BALLARD’, ‘DARNELL WEBER’, ‘SYLVIE CHARLES’, ‘VINAY PADILLA’, ‘MEREDITH SANTIAGO’, ‘ANDRE MCCARTY’, ‘LORENA HODSON’, ‘ISAAC VU’]
Ages: [‘27’, ‘30’, ‘43’, ‘35’, ‘22’, ‘24’, ‘51’, ‘19’, ‘65’, ‘34’]
BMI: [‘31.1’, ‘22.4’, ‘25.3’, ‘20.6’, ‘22.1’, ‘26.9’, ‘29.3’, ‘22.7’, ‘33.1’, ‘24.8’]
Insurance Costs: [’$7010.0’, ‘$4050.0’, ‘$12060.0’, ‘$7500.0’, ‘$3022.0’, ‘$4620.0’, ‘$16330.0’, ‘$2900.0’, ‘$19370.0’, ‘$7045.0’]

And the task is to 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

I have tried different ways such as:
for name in names:
for age in ages:
for bmi in bmis:
for insurance_cost in insurance_costs:
print(name + " is " + str(age) + " year olds with a BMI of " + str(bmi) + " and an insurance cost of " + insurance_cost + “.”)

and
for name, age, bmi, insurance_cost in names, ages, bmis, insurance_costs:
return "{} is {} year olds with a BMI of {} and an insurance cost of {}. ".format(name, age, bmi, insurance_cost)

Both of my ways are wrong. So I wonder what code I should put for this for loop.

Thanks a lot and happy coding! :slight_smile:

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-strings/projects/ds-python-strings-project

Here is the link

Hello! Since each element in the lists corresponds to the other lists; the first element of the names list is to do with Marina Allison, the first element of the ages list is to do with Marina Allison, etc, you could consider using range(). I will give this as a first hint; if you need more, just ask!

1 Like

Thank you so much for your hint. But I’m just wondering how can I connect all these lists into a for loop? Could you please give me another hint? Thanks a lot! :slight_smile:

You can either use range() and list indexing, or you can use this approach (which still requires list indexing):

x = [9,8]
y = [8,9]
for i in x,y:#this is similar to using zip. The variable i holds a list
#which contains the first values of x&y on, the first iteration,
#the second values of x&y on the second iteration, and so forth
  print(i)
#prints:
#[9,8]-the value of i on the first iteration
#[8,9]-the value of i on the second iteration
1 Like

Thank you for your reply, and I’ve been trying the way you’re telling. But seem I’m stuck…
And I came up the following code, but unfortunately, I’m still bit confused.

for i in range(len(names)), range(len(ages)), range(len(bmis)), range(len(insurance_costs)):
  print(names[i] + " is " + str(ages[i]) + " year olds with a BMI of " + str(bmis[i]) + " and an insurance cost of " + insurance_costs[i] + ".")

I do apologize if I sound stupid for you but would be nice if you could give more hints…
Thank you so much and happy new year.

With the last code snippet, you are pretty close. See my last post about what happens when you do for i in x,y. What is i? What data type is i? You have used multiple range(len(x)), but why? What does range(len(x)) return? If each list is the same length, does that hint towards anything? Same, uni, one?
I’ll leave it at that, but if you need more, I’m more than happy to help! And happy New Year to you as well!

Please help with other hints

Please don’t cross-post to multiple topics as mentioned in the community guidelines-Guidelines - Codecademy Forums

so here is my solution:

please let me know if there is a better approach to the problem

for d in data:

name,age,bmi,cost = d[0:]

print({name} + " is " + {age} + " old " + "with a bmi of " + {bmi} + " and an insurance cost of "+ {cost})

1 Like

in my case it worked only when I iterated the medical_records_clean list

for record in medical_records_clean: print(str(record[0]) + " is " + str(record[1]) + " years old with a BMI of " + str(record[2]) + " and insurance cost of " + str(record[3]))

I don´t really thing, that it was we were expected to do, but many ways lead to Rome :slight_smile:

2 Likes

i did the same, and can’t see why we couldn’t use it… it’s in our own code :nerd_face:

for i in medical_records_clean:
  print(i[0]+" is "+i[1]+" years old with a BMI of "+i[2]+" and an insurance cost of "+i[3])
1 Like

Hello,

I have the same question.
Can you please provide more hints?

Hello! If you still need help with this, could you post the code you currently have, please? Make sure to format it correctly.

Hi, there might be a better way to do this, but the below worked for me. For me personally, isolating the first name was the most challenging. Hope this helps!

for index in medical_records_clean:
  first_name = []
  first_name.append(index[0].split(" "))
  first_name_only = []
  for first in first_name:
    first_name_only.append((first[0]))
  print(first_name_only[0] + " is " + index[1] + " with a BMI of " + index[2] + " and an insurance cost of " + index[3])
2 Likes

This was the best answer given here. In fact, the most difficult part in this task is to isolate the first name.

The solution that worked best for me while also keeping the code clean was using a for loop (with the medical_records_clean list) and .format (I set up the format to be variable = temporary_variable[index] for name, age, bmi, and insurance_cost). Hope this helps!

1 Like