0

Here's the program. The program converts from Kilogram to Pounds and vice-versa.

pref = input("Hello, This is a program to convert 1. Mass, 2. Length, 3. Speed, 4. Temperature, 5. Currency and 6. Date \n Enter your option in NUMBER")

if pref == 1. or 1:
     pref1 = input("1. Kg to lb \n 2. Lb to kgs")

if pref1 == 1:
    Kgs = input("Enter weight in Kg")
    Lbs = float(Kgs)*2.20462
    print(str(Lbs)) 
elif pref1 == 2:
    Lbs = input("Enter weight in lbs")
    Kgs = float(Lbs)/2.20462
    print(str(Kgs))
else:
    exit()

However, it exits when I type a value into the program for the second time, i.e. when the program asks the user to enter their preference of whether they want to convert from Pound to Kilogram or vice-versa.\

Could someone please point out my mistake?

2 Answers 2

1

When you get your input, the type of it will be str. Since you are only comparing to ints, you will end up in your else clause. Try converting the result of input to int

e.g.

pref = int(input("Hello, This is a program to convert 1. Mass, 2. Length, 3. Speed, 4. Temperature, 5. Currency and 6. Date \n Enter your option in NUMBER"))

if (pref == 1.) or (pref == 1):
     pref1 = int(input("1. Kg to lb \n 2. Lb to kgs"))
....

As pointed out in the comments, you also should check if (pref == 1) instead of if pref == 1. or 1 since pref is converted to an integer in my answer anyway

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

1 Comment

Still wrong. Python sees if pref == 1. or 1: as if (pref == 1.) or 1: and that will always evaluate to True.
1

The issue is that pref, when received from input, is a string and not an integer.

Change all your if statements to :

if int(pref1) == ...

That would solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.