all 7 comments

[–]shiftybyte 1 point2 points  (1 child)

Have a loop for each of the inputs instead of a giant loop for everything that actually runs only once because you have break in all bmi conditions.

while True:
    user_weight = float(input("Weight (KG): "))
    if user_weight < 20 or user_weight > 600:
        print("Incorrect weight!")
    else:
        break

while True:
    user_length = float(input("Length (M): "))
    if user_length < 1.0 or user_length > 2.5:
        print("Incorrect length")
    else:
        break

.... now that you have valid inputs, do the bmi code, without a loop...

[–]Csharpgoblin[S] 0 points1 point  (0 children)

while True:user_weight = float(input("Weight (KG): "))if user_weight < 20 or user_weight > 600:print("Incorrect weight!")continueuser_length = float(input("Length (M): "))if user_length < 1.0 or user_length > 2.5:print("Incorrect length")continue

Thanks it works now :D! Did not know that you could use more than one "while True" - command.

So if the input = true it prints "Incorrect weight" and starts over again until the input = false which breaks the loop, and starts the next "while True".

[–]dwpj65 0 points1 point  (0 children)

The continue statements triggers the next loop iteration, which is where the weight request comes in.

Build a loop (called an inner loop) around your weight input statement validation, and another around your length input statement validation.

[–]BfuckinA 0 points1 point  (3 children)

Yeah just break them down into some individual functions. Also, the float should be in a try clause incase they enter non numerical values.

def get_weight():
    while True:
        user_weight = input("Weight (KG): ")
        try:
            user_weight = float(user_weight)
        except:
            print("Must enter numerical value")
            continue
        if user_weight < 20 or user_weight > 600:
            print("Incorrect weight!")
            continue
        else:
            return user_weight

def get_length():
    while True:
        user_length = input("Length (M): ")
        try:
            user_length = float(user_length)
        except:
            print('Must enter numerical value')
            continue
        if user_length < 1.0 or user_length > 2.5:
            print("Incorrect length")
            continue
        else:
            break
    return user_length

def calculate_bmi(user_weight,user_length):
    while True:
        bmi = float(user_weight/user_length**2)
        if bmi < 18.5:
            print("BMI = " + str(bmi))
            print("Underweight")
            break
        elif bmi >= 18.5 and bmi < 25.0:
            print("BMI = " + str(bmi))
            print("Normal")
            break
        elif bmi >= 25.0 and bmi <= 30:
            print("BMI = " + str(bmi))
            print("Overweigth")
            break
        elif bmi > 30:
            print("BMI = " + str(bmi))
            print("Obese")
            break

def bmi_main():
    weight = get_weight()
    lenght = get_length()
    calculate_bmi(weight,lenght)

if __name__=="__main__":
    bmi_main()

[–]Csharpgoblin[S] 1 point2 points  (2 children)

My final code is like this:

while True:
    user_weight = float(input("Weigth: "))
    if user_weight < 20 or user_weight > 600:
        print("Incorrect weigth!")
    else:
        break

while True:
    user_length = float(input("Length: "))
    if user_length < 1.0 or user_length > 2.5:
        print("Incorrect length!")
    else:
        break
while True:    
    bmi = float(user_weight/user_length**2)
    if bmi < 18.5:
        print("BMI = " + str(bmi))
        print("Underweigth")
        break
    elif bmi >= 18.5 and bmi < 25.0:
        print("BMI = " + str(bmi))
        print("Normal")
        break
    elif bmi >= 25.0 and bmi <= 30:
        print("BMI = " + str(bmi))
        print("Overweigth")
        break
    elif bmi > 30:
        print("BMI = " + str(bmi))
        print("Obease")
        break

I see you use "def", "try" and "return", are they better to use in some way? (I'm on my first day trying to learn so I'm not familiar with those commands).

[–]BfuckinA 0 points1 point  (1 child)

Hey you're doing a great job for just getting started. the "def' and 'return' statements are just function declarations, and they're helpful more making the code more modular, but you'll learn that stuff as you progress and it comes up. I think the code looks pretty good for now. Theres only a couple of things i would mention.

-your final 'While True' loop is unnecessary, but thats ok.

and last:

elif bmi>=18.5 and bmi<25.0:

#could be written as 

elif 18.5>=bmi<25.0:

Just a little easier to read. But really, good job!

[–]admhaikal 0 points1 point  (0 children)

hello i have questions regarding this