 # Assignment of Week 3 - Problem 1 - JUNK value persisting if Input not correct

I am using following code for resolution of Problem 1

``````weight=input("Enter your weight (in kgs): ")`
pref=input("What is your preferred unit of height? Type \"F\" for feet and \"M\" for meters: ")
if pref=="F":
feet=input("You will enter your height as feet and inches. First enter feet: ")
inches=input("Now enter inches: ")
meters= (float(feet)*0.3048) + (float(inches)*0.0254)
elif pref=="M":
meters=input("What is your height in meters: ")

BMI=float(weight)/(float(meters))**2
if BMI<18.5:
print("BMI = ", BMI, "Weight=",float(weight), "Height(mts)", float(meters), "feet=", feet, "inches=", inches, " :UNDERWEIGHT")
elif BMI>=18.5 and BMI<25:
print("BMI = ", BMI, "Weight=",float(weight), "Height(mts)", float(meters), "feet=", feet, "inches=", inches, " :NORMAL")
elif BMI>=25 and BMI<30:
print("BMI = ", BMI, "Weight=",float(weight), "Height(mts)", float(meters), "feet=", feet, "inches=", inches, " :OVERWEIGHT")
else:
print("BMI = ", BMI, "Weight=",float(weight), "Height(mts)", float(meters), "feet=", feet, "inches=", inches, " :VERY-OVERWEIGHT")
``````

I am getting a junk value returned in feet and inches variable. Even if I donot press correct input at the terminal while entering the option of F and M then also the same junk values get into code execution and meters is converted badly and give me some junk value of BMI and hence the BMI result. That’s why I clearly printed the value of feet and inches in that case so that I can know what values system is fetching for them.

Is there a way to eliminate the junk in this case?

1 Like

@neerajarora.india

Initialize feet and inches outside the if scope.
`feet = inches = 0`
Then you will not see junk values in them.

``````weight=input("Enter your weight (in kgs): ")
pref=input("What is your preferred unit of height? Type \"F\" for feet and \"M\" for meters: ")
feet = inches = 0
if pref=="F":
feet=input("You will enter your height as feet and inches. First enter feet: ")
inches=input("Now enter inches: ")
meters= (float(feet)*0.3048) + (float(inches)*0.0254)
elif pref=="M":
meters=input("What is your height in meters: ")
``````
1 Like

This was more or less my code as well , but whenever i gave some other alphabets instead of F or M , it still continued with the loop and ends up printing some random values for BMI . I declared a separate list consisting of alphabets except F and M . and if my variable encountered that i told it to print something .

Is there any other neat way to do it instead of writing the entire alphabets list ?

It is your code only, except for creating the variable outside the scope of if statement. By doing so, the scope of the variable is extended and they will contain the values entered by the user when user has selected F OR if user has selected M, they would have the initialised value of 0.

Please try the code shown above and observe the output.

Well…I tried the code as suggested by you. The only fix still remains is that what happens when user enters neither of ‘F’ nor ‘M’. The code in that case errors out showing the remaining BMI calculation code divisibility issue.
Well, my intent of posting this was to understand how and why python absorbs any junk value of the variable that has not been initialized at all by user input unlike other compiler based languages like C, C++ etc. I was expecting python to throw error then and there.
Although I have fixed this code by adding a simple flag for handling this exception as below:

``````vldInput='N' #Initialized a flag to check user input
weight=input("Enter your weight (in kgs): ")
pref=input("What is your preferred unit of height? Type \"F\" for feet and \"M\" for meters: ")
if pref=="F":
feet=input("You will enter your height as feet and inches. First enter feet: ")
inches=input("Now enter inches: ")
meters= (float(feet)*0.3048) + (float(inches)*0.0254)
vldInput='Y' #Updated the valid user input
elif pref=="M":
meters=input("What is your height in meters: ")
vldInput='Y' #updated the valid user input

#Added all the earlier BMI calculation code under if-else block
if vldInput == 'Y':
BMI=float(weight)/(float(meters))**2
if BMI<18.5:
print("BMI = ", BMI, " :UNDERWEIGHT")
elif BMI>=18.5 and BMI<25:
print("BMI = ", BMI, " :NORMAL")
elif BMI>=25 and BMI<30:
print("BMI = ", BMI, " :OVERWEIGHT")
else:
print("BMI = ", BMI, " :VERY-OVERWEIGHT")
else:
print("Not a Valid Option")
``````

Working perfectly alright now.