Home > Back-end >  How can I determine the BMI for an n amount of students, whose weights will be randomly generated in
How can I determine the BMI for an n amount of students, whose weights will be randomly generated in

Time:10-28

I'm stuck on this part of my homework that says: "Generate weights for 100 students in the range of 100 to 300 pounds. Determinethe BMI for each student. Determine BMI my status for each student."

I already have my def function to calculate the BMI status and a BMI formula, but I can't figure out a way to actually generate the weights within that range for those number of students, or how to determine the BMI for each student using a for loop.

Also, wouldn't I have to consider/generate random numbers for their height(feet, inches)to incorporate in the BMI formula? How would I do that..

Please help !

I've already set up my function for BMI status that works fine. Ive atleast attempted to code the concept for the part i've been having trouble with(see below):

for x in range (100):
weight = range(randint(100,300)
inches = range(randint(0,12))
feet = range(randint(1,6)
height=feet*12   inches
bmi= 703*weight/height**2

CodePudding user response:

This is how you can genrate random weight

import random
def rnd():
  arr = random.sample(range(100,300), 100)
  return arr

weight_list =rnd()
print(weight_list)

here is your entire BMI calculator

import random
def bmi_cal():
    bmiLst = []
    weight = random.sample(range(100,300), 100)
    feet = [random.randint(5,7) for _ in range(1,100)]
    inches = [random.randint(0,12) for _ in range(1,100)]
    for i in range(0, 99):
        height = (feet[i]*12) inches[i]
        bmi= 703*(weight[i]/height**2)
        bmiLst.append(bmi)
    return bmiLst
print(bmi_cal())

CodePudding user response:

Yes, you would have to generate random weights in between 100 and 300. Here's a python function for the same,

import random
weights = []
feet = []
inches = []
for x in range(100):
    weights.append(random.randint(100, 300))
    feet.append(random.randint(0, 12))
    inches.append(random.randint(1, 6))

bmis = bmi_calculator(weights, feet, inches)

The bmi_calculator function is the one you wrote taking in the list of weights, or individual weight.

This will give you a list of 100 weights in between 100 and 300.

And either in your HW, you have been given a list of heights or you have to generate that as well. You can then use these two lists, one of weights and one of height, to get your bmi function running.

Assuming you are new to programming, here's a couple suggestions, Your HW problem said that you need to generate weights in between 100 and 300. If you simply google python generate numbers in between 100 and 300 You would have gotten your answer.

Another small suggestion, try to use variables as much as you can. It is good practice.

Welcome to programming :)

  • Related