Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BMI code
#1
hi recently i started learning python my first project is BMI calculator after coding in (python 3.11) when i execute my code i get error saying float' object cannot be interpreted as an integer any help is much appreciated
my BMI coding:
weight = float(input('your weight in kg?:'))
height = float(input ('your height in cm?:'))
BMI = weight/(height/100)**2
print ('your BMI is', BMI)
if BMI < 18.5:
    print  ("under weight")
if BMI > 25:
    print  ("over weight")
if BMI in range(18.5 , 24.9):
    print  ("your BMI is IDEAL")
thanks in advance
deanhystad write Sep-05-2023, 03:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You cannot use float numbers for start, stop, step in range(start, stop, step). This is what is giving the error. Also, range does not do what you think it does. You should read up about range() because you'll be using it a lot.

Do you need this?
if BMI in range(18.5 , 24.9):
    print  ("your BMI is IDEAL")
To get here we already know that the bmi >= 18.5 and that bmi <= 25 because the earlier if statements already caught all under and overweight cases. Think about the problem first, then think about the code.
Reply
#3
(Sep-05-2023, 03:49 PM)deanhystad Wrote: You cannot use float numbers for start, stop, step in range(start, stop, step). This is what is giving the error. Also, range does not do what you think it does. You should read up about range() because you'll be using it a lot.

Do you need this?
if BMI in range(18.5 , 24.9):
    print  ("your BMI is IDEAL")
To get here we already know that the bmi >= 18.5 and that bmi <= 25 because the earlier if statements already caught all under and overweight cases. Think about the problem first, then think about the code.
tnx Mod you're right it was simple than i thought range() function was really bad i only needed <= and >= .thanks for advice
Reply


Forum Jump:

User Panel Messages

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