Home > database >  weight conversion program failed
weight conversion program failed

Time:11-19

weight = float(input("weight: "))
unit = input("(K)g or (L)bs: ")

if unit.upper == "K":
    converted = weight*2.2
    print("Weight in Lbs: "   str(converted))
else:
    converted = weight/2.2
    print("Weight in kgs: "   str(converted))
print("Conversion done")

result:

weight: 15
(K)g or (L)bs: k
Weight in kgs: 6.8181818181818175
Conversion done

I am a beginner and trying to convert the weight but only else block is executing not the if block. Why?

CodePudding user response:

weight = float(input("Enter weight: "))
unit = input("Entered Value is (K)g or  (L)bs: ")

if unit.upper() == "K":
    converted = weight*2.2
    print("Weight in Lbs: "   str(converted))
else:
    converted = weight/2.2
    print("Weight in kgs: "    str(converted))
print("Conversion done")

You need to use if unit.upper() instead of unit.upper otherwise it will always go to the else statement as if will deem input to be false

CodePudding user response:

You need to use unit.upper() with the ().

str.upper is the name of the method, but you want to actually call the method, which requires the parens.

As you have it, if unit.upper == 'K' is checking 'is our method unit.upper equal to the string capital K'?

  • Related