I am coding a BMI calculator in python and wanted to add exception handling. I created two functions 1. a height converter that converts the height to feet or meters depending on the user's input, and a weight converter that converts the user's weight to kg or pounds depending on the input. The def height_converter(h) and weight_converter(w) functions don't restart when the user types in the wrong input, unlike the codes below it which asks only for the weight and height. Also, the BMI variable returns an error, I don't know what to do again
# BMI CALCULATOR IN PYTHON
import os
from datetime import datetime
# define our clear function
def clear():
# for windows
if os.name == 'nt':
os.system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = os.system('clear')
def weight_converter(w):
while True:
try:
global converted
converted = 0
weight_unit = input("What is the weight unit Kgs or Lbs: ")
if weight_unit.upper() == "KG":
converted = w / 1
print("weight in kg is: ", converted)
elif weight_unit.upper() == "LBS":
converted = w / 2.2
print("weight in kg is: ", converted)
else:
raise ValueError(weight_unit)
break
except (ValueError, IOError, IndexError):
print("ERROR")
return converted
def height_converter(h):
while True:
try:
height_unit = input("what is the height unit meters or feet: ")
if height_unit.upper() == "METERS":
converted = h / 1
print("height in meters is: ", converted)
elif height_unit.upper() == "FEET":
converted = h / 3.281
print("height in meters is: ", converted)
break
except(ValueError,IOError,IndexError):
print("ERROR")
return converted
while True:
try:
age = input("How old are you? ")
age = int(age)
weight = input("What is your weight: ")
weight = float(weight)
wconverted = weight_converter(weight)
height = input("What is your height: ")
height = float(height)
hconverted = height_converter(height)
break
except ValueError:
# os.system(clock_settime)
print("No valid integer! Please try again ...")
clear()
BMI = float(wconverted / (hconverted ** 2))
print("Your BMI is: ", BMI, "as of ", date)
print("You are using a", os.name, "system")