0

I am trying to make a code that calculates the BMI of individuals that the user inputs. Here is where I am stuck, using a second loop the program should traverse the array of body mass indices and call another function that accepts the body mass index(BMI) as a parameter and returns whether the individual is underweight, normal weight or overweight, finally I want to count number of individuals in each category. I keep getting an error typeerror: ‘>’ not supported between instances of ‘str’ and ‘int’ Solution. Honestly I don't even know if I'm doing it right at all to get the out put I want. Thank you for any help.

Below if what I've got:

     individuals = list()
    for i in range((int(input("Please enter the number of individuals you would like calculate BMIs for:")))):
        user = str(input("Please enter the names of the individuals one at a time: "))
        individuals.append(user)
    BMIs = []
    for user in individuals:
        print("Input for", user)
        height = int(input(user + ", in inches, how tall are they? "))
        weight = int(input(user + ", in pounds, how much do they weigh? "))
        BMIs.append(user  + "s, BMI is: " + str(weight * 703/height**2))
    
    for BMI in BMIs:
        if ( BMI <18.5):
            print(BMI, "underweight")
        elif ( BMI >= 18.5 and BMI < 30):
            print(BMI,"normal")
        elif ( BMI >=30):
            print(BMI,"severely overweight")
PintSized
  • 1
  • 1
  • 4
    The title should describe your _problem_, not what kind of thing you were writing when you encountered that problem. (Similarly, a good [mre] is simplified to be the _shortest possible code_ that produces the same problem when run without changes; if you can produce the problem without user input or prompting, you should take the user input and the prompting out of your code before posting). – Charles Duffy Dec 15 '20 at 00:45

2 Answers2

0

Here you are appending strings to a list BMIs:

BMIs.append(user  + "s, BMI is: " + str(weight * 703/height**2))

So then when you do a comparison Python throws you an error. What you need to do instead is to use a dictionary:

    BMI = {}  # Create an empty dict here
    for user in individuals:
        print("Input for", user)
        height = int(input(user + ", in inches, how tall are they? "))
        weight = int(input(user + ", in pounds, how much do they weigh? "))
        BMI[user] = weight * 703/height**2

Then you can do comparisons using dictionary values:

    for key, value in BMI.items():
        if BMI[key] < 18.5:
            print(f'{key} with BMI of {value} is underweight')
        elif BMI[key] >= 18.5 and BMI[key] < 30:
            print(f'{key} with BMI of {value} has normal weight.')
        elif BMI[key] >=30:
            print(f'{key} with BMI of {value} is overweight.')

Also, you don't need to use brackets in if statements in Python.

NotAName
  • 3,821
  • 2
  • 29
  • 44
0

According to your error you are trying to compare a str(string) variable with an int(integer) one. This is because when you are iteratating through your BMIs list, all your BMI values have the form: "user's, BMI is number"

So the comparison that takes place is: "user's, BMI is number" > int(number) You are comparing a whole string to a number and python doesn't understand this.

You need to parse your string and extract your number and then compare it.

One possible way to do this is: BMI[BMI.rindex(" ")::]

So this loop

for BMI in BMIs:
        if ( BMI <18.5):
            print(BMI, "underweight")
        elif ( BMI >= 18.5 and BMI < 30):
            print(BMI,"normal")
        elif ( BMI >=30):
            print(BMI,"severely overweight")

is transformed to:

for BMI in BMIs:
        if (int(BMI[BMI.rindex(" ")::]) <18.5):
            print(BMI, "underweight")
        elif (int(BMI[BMI.rindex(" ")::]) >= 18.5 and int(BMI[BMI.rindex(" ")::]) < 30):
            print(BMI,"normal")
        elif (int([BMI.rindex(" ")::]) >=30):
            print(BMI,"severely overweight")

The rindex method returns the index of the last occurence of the string parameter inside the string. The string[::] notation is a simple string slice.