Python Forum
IMC Calculator but with a list of patients
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IMC Calculator but with a list of patients
#1
Sad 
I created a BMI and MB calculator and everything is working fine! But now, I would like to apply this calculator to a list a patient and show result for all of them. How could I apply this a list? How could I move the whole code into a function and call that function for each patient in the list? I am new to python and I can’t figure this out.

I have the idea to do a loop of this kind but I don’t know how Cry

for patient in patients:
imc = calcul_imc(patients[patient]["poids_patient"], patients[patient]["taille_patient"])
Here is my code; hope you’ll understand it even if there is some French aha!

patients = {1: {"nom_patient":"Olivier", "sexe_patient":'H', "age_patient":35, "poids_patient":78, "taille_patient":1.85},2: 
            {"nom_patient":"Claire", "sexe_patient":'F', "age_patient":28, "poids_patient":64, "taille_patient":1.74},3: 
            {"nom_patient":"Damien", "sexe_patient":'H', "aage_patient":65, "poids_patient":89, "taille_patient":1.71}};

def calcul_imc(poids, taille):
    return poids/(taille**2)

nom_patient = str(input("Entrez votre nom:"))
poids_patient = int(input("Entrez votre poids en kg:"))
taille_patient = float(input("Entrez votre taille en m:"))
age_patient = int(input("Entrez votre age:"))
sexe_patient = str(input("Êtes-vous un homme ou une femme? (H/F)"))


imc = calcul_imc(poids_patient, taille_patient)

if imc <= 18.99:
    print("Vous êtes en maigreur, le risque de maladies est accru.")
elif imc < 24.99:
    print("Vous êtes en poids normal, le risque de maladies est faible.")
elif imc < 29.99:
    print("Vous êtes embonpoint, le risque de maladies est accru.")
else :   
    print("Vous êtes en obésité modérée, le risque de maladies est élevé.")

if sexe_patient == "H":
    sexe_patient = True
elif sexe_patient == "F":
    sexe_patient = False
else:
    print("Oupsie! L'entrée doit être Oui ou Non pour pouvoir calculer ")
    quit()
    
if sexe_patient:
    mb = 3.707*poids_patient + 492.3*taille_patient - 6.673 * age_patient + 77.607
else:
    mb = 9.740*poids_patient+172.9*taille_patient-4.737*age_patient + 667.051

print("Votre métabolisme de base est", mb)
Reply
#2
Something like this?
for patient in patients:
    # call imc function
    # print result
It will be easier to do this if patients is a list instead of a dictionary.
patients = [
    {"nom_patient":"Olivier", "sexe_patient":'H', "age_patient":35, "poids_patient":78, "taille_patient":1.85},
    {"nom_patient":"Claire", "sexe_patient":'F', "age_patient":28, "poids_patient":64, "taille_patient":1.74},
    {"nom_patient":"Damien", "sexe_patient":'H', "aage_patient":65, "poids_patient":89, "taille_patient":1.71}
]
If 1, 2, 3 is important information, like a patient ID number, you will have to loop through the patients like this:
for id, patient in patients.items():
    # call imc function
    # print result
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020