0

I understand this question has been asked before but I cant find a working solution. I'm new to Python and need to create a BMI calculator that will allow the user to enter multiple BMI's, but I can't get it to loop back to the beginning sucessfully. I've tried 2 ways and neither work.

Here's what I have for the first one, but this one wont run as "continue not in loop": `

while True: print("BMI Calculator") weight = float(input("\nPlease enter your weight in KG: ")) height = float(input("\nPlease enter your height in metres: ")) bmi = weight/(height*height) if bmi <= 18.5: print("Your BMI is", bmi,"which means you are underweight.") elif bmi > 18.5 and bmi < 25: print("Your BMI is: ", bmi, "which means you are normal") elif bmi > 25 and bmi < 30: print("Your BMI is: ", bmi, "which means you are overweight") elif bmi > 30: print("Your BMI is: ", bmi, "which means you are obese") else: print("There was an error with your input, Sorry.") while True: answer = input("Would you like to enter another? key y/n: ") if answer in ("y", "n"): break print("Invalid Input") if answer == "y": continue else: input("\nPress the enter key to exit") break 

`

The other one I have is this, but it prints BMI Calculator and then stops: `

def start(): print("\nBMI Calculator") weight = float(input("\nPlease enter your weight in KG: ")) height = float(input("\nPlease enter your height in metres: ")) bmi = weight/(height*height) if bmi <= 18.5: print("Your BMI is", bmi,"which means you are underweight.") elif bmi > 18.5 and bmi < 25: print("Your BMI is: ", bmi, "which means you are normal") elif bmi > 25 and bmi < 30: print("Your BMI is: ", bmi, "which means you are overweight") elif bmi > 30: print("Your BMI is: ", bmi, "which means you are obese") else: print("There was an error with your input, Sorry.") answer = input("Would you like to enter another? key y/n: ") while answer == "y": start() answer = None if answer == "n": input("\nPress the enter key to exit") 

`

3
  • 1
    Is that actually what your code looks like or did the indentation get messed up? Commented Nov 10, 2017 at 17:20
  • You need to put all your code inside the loop, and break out when the user said so. Commented Nov 10, 2017 at 17:30
  • indentation messed up Commented Nov 10, 2017 at 17:35

3 Answers 3

4

Your problem here is all about the indentation levels, Laura. Python knows that something is inside a block of code if the consecutive lines have all the same indentation(differently from C or Java, where blocks are delimited by opening and closing brackets).

Your code should look something like this:

while True: print("BMI Calculator") weight = float(input("\nPlease enter your weight in KG: ")) height = float(input("\nPlease enter your height in metres: ")) bmi = weight/(height*height) if bmi <= 18.5: print("Your BMI is", bmi,"which means you are underweight.") elif bmi > 18.5 and bmi < 25: print("Your BMI is: ", bmi, "which means you are normal") elif bmi > 25 and bmi < 30: print("Your BMI is: ", bmi, "which means you are overweight") elif bmi > 30: print("Your BMI is: ", bmi, "which means you are obese") else: print("There was an error with your input, Sorry.") answer = input("Would you like to enter another? key y/n: ") if answer not in ("y", "n"): print("Invalid Input") break if answer == "y": continue else: input("\nPress the enter key to exit") break 

In this snippet of code I have changed the boolean test and the instruction order from:

if answer in ("y", "n"): break print("Invalid Input") 

to

if answer not in ("y", "n"): print("Invalid Input") break 

If you break from a loop, the following lines of code in that loop are not executed. Also, the comparison you were doing would always return True because the answer would be in ("y", "n"). I also removed the last loop because it makes more sense that way.

As for the second code, the only thing that function start() is doing, is print("\nBMI Calculator"), again because of indentation levels.

Hope that helps :)

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like in the second example you need to put the yes no question in the while loop. Something like this should work.

def start(): print("\nBMI Calculator") while True: weight = float(input("\nPlease enter your weight in KG: ")) height = float(input("\nPlease enter your height in metres: ")) bmi = weight/(height*height) if bmi <= 18.5: print("Your BMI is", bmi,"which means you are underweight.") elif bmi > 18.5 and bmi < 25: print("Your BMI is: ", bmi, "which means you are normal") elif bmi > 25 and bmi < 30: print("Your BMI is: ", bmi, "which means you are overweight") elif bmi > 30: print("Your BMI is: ", bmi, "which means you are obese") else: print("There was an error with your input, Sorry.") answer = input("Would you like to enter another? key y/n: ") while answer == "y": start() answer = None if answer == "n": exit() start() 

I didn't put an else statement in the y/n question but that should be a simple fix, it looks like the statement you had written before would work fine.

Comments

0

First Thing :

you are facing issue causes of indentation :

Second Thing :

For second function , I have some suggestion for you:

Instead of print use return there :

The point of functions in general is to take in inputs and return something. The return statement causes your function to exit and hand back a value to its caller.

and use python .format() method

Here is your second code with some modification :

def start(): print("\nBMI Calculator") weight = float(input("\nPlease enter your weight in KG: ")) height = float(input("\nPlease enter your height in metres: ")) bmi = weight/(height*height) if bmi <= 18.5: return "Your BMI is : {} which means you are underweight".format(bmi) elif bmi > 18.5 and bmi < 25: return "Your BMI is : {} which means you are normal".format(bmi) elif bmi > 25 and bmi < 30: return "Your BMI is : {} which means you are overweight".format(bmi) elif bmi > 30: return "Your BMI is : {} which means you are obese".format(bmi) else: return "There was an error with your input, Sorry." print(start()) answer = input("Would you like to enter another? key y/n: ") if answer == "y": start() elif answer == "n": input("\nPress the enter key to exit") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.