How do I fix my unit converter in python?

58 views Asked by At

Every time I try to convert it, it only shows the conversion from in. to mm.(aka the 1st expression)I would really appreciate it if you can help because I spent a long time.

a = input('Original Amount:')
b = input('Original Unit: ')
c = input('What to convert to: ')

if b == 'in.' or 'in':
    if c == 'mm.' or 'mm':
        print(float(a) * float(25.4))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(2.54))
    elif c == 'ft.' or 'ft':
        print(float(a) / float(12))
    elif c == 'yd.' or 'yd':
        print(float(a) / float(36))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(63360))
    elif c == 'km.' or 'km':
        print(float(a) / float(8500.8))
    elif c == 'm.' or 'm':
        print(float(a) / float(39.37))
    elif c == 'rod.' or 'rod':
        print(float(a) / float(4.55))

elif b == 'ft.' or 'ft':
    if c == 'in.' or 'in':
        print(float(a) * float(12))
    elif c == 'yd.' or 'yd':
        print(float(a) / float(3))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(5280))
    elif c == 'rod.' or 'rod':
        print(float(a) / float(16.5))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(304.8))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(30.48))
    elif c == 'm.' or 'm':
        print(float(a) / float(27.3))
    elif c == 'km.' or 'km':
        print(float(a) / float(3281))

elif b == 'yd.' or 'yd':
    if c == 'in.' or 'in':
        print(float(a) * float(36))
    elif c == 'ft.' or 'ft':
        print(float(a) * float(3))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(1760))
    elif c == 'rod.' or 'rod':
        print(float(a) / float(5.5))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(914.4))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(91.44))
    elif c == 'm.' or 'm':
        print(float(a) * float(0.91))
    elif c == 'km.' or 'km':
        print(float(a) / float(1094))

elif b == 'mi.' or 'mi':
    if c == 'in.' or 'in':
        print(float(a) * float(63360))
    elif c == 'ft.' or 'ft':
        print(float(a) * float(5280))
    elif c == 'yd.' or 'yd':
        print(float(a) * float(1760))
    elif c == 'rod.' or 'rod':
        print(float(a) * float(320))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(1609344))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(160934.4))
    elif c == 'm.' or 'm':
        print(float(a) * float(1609.34))
    elif c == 'km.' or 'km':
        print(float(a) * float(1.61))

elif b == 'rod.' or 'rod':
    if c == 'in.' or 'in':
        print(float(a) * float(198))
    elif c == 'ft.' or 'ft':
        print(float(a) * float(16.5))
    elif c == 'yd.' or 'yd':
        print(float(a) * float(5.5))
    elif c == 'mi.' or 'mi':
        print(float(a) * float(320))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(5029.2))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(502.92))
    elif c == 'm.' or 'm':
        print(float(a) * float(5))
    elif c == 'km.' or 'km':
        print(float(a) * float(198.8))

elif b == 'mm.' or 'mm':
    if c == 'in.' or 'in':
        print(float(a) / float(25.4))
    elif c == 'ft.' or 'ft':
        print(float(a) / float(304.8))
    elif c == 'yd.' or 'yd':
        print(float(a) / float(914.4))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(6.21))
    elif c == 'rod.' or 'rod':
        print(float(a) / float(5029))
    elif c == 'cm.' or 'cm':
        print(float(a) / float(10))
    elif c == 'm.' or 'm':
        print(float(a) / float(1000))
    elif c == 'km.' or 'km':
        print(float(a) / float(1000000))

elif b == 'cm.' or 'cm':
    if c == 'in.' or 'in':
        print(float(a) / 2.54)
    elif c == 'ft.' or 'ft':
        print(float(a) / float(30.48))
    elif c == 'yd.' or 'yd':
        print(float(a) / float(91.44))
    elif c == 'mi.' or 'mi':
        print(float(a) * float(6.21))
    elif c == 'rod.' or 'rod':
        print(float(a) * float(502.9))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(10))
    elif c == 'm.' or 'm':
        print(float(a) / float(100))
    elif c == 'km.' or 'km':
        print(float(a) / float(100000))

elif b == 'm.' or 'm':
    if c == 'in.' or 'in':
        print(float(a) * float(39.37))
    elif c == 'ft.' or 'ft':
       print(float(a) * float(3.281))
    elif c == 'yd.' or 'yd':
        print(float(a) * float(1.094))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(1609))
    elif c == 'rod.' or 'rod':
        print(float(a) / float(5.029))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(1000))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(100))
    elif c == 'km.' or 'km':
        print(float(a) / float(1000))

elif b == 'km.' or 'km':
    if c == 'in.' or 'in':
        print(float(a) * float(39370))
    elif c == 'ft.' or 'ft':
        print(float(a) * float(3281))
    elif c == 'yd.' or 'yd':
        print(float(a) * float(1094))
    elif c == 'mi.' or 'mi':
        print(float(a) / float(1.609))
    elif c == 'rod.' or 'rod':
        print(float(a) * float(198.8))
    elif c == 'mm.' or 'mm':
        print(float(a) * float(1000000))
    elif c == 'cm.' or 'cm':
        print(float(a) * float(100000))
    elif c == 'm.' or 'm':
        print(float(a) * float(1000))

elif b == 'oz.' or 'oz':
    if c == 'lb.' or 'lb':
        print(float(a) / float(16))
    elif c == 'USton.' or 'USton':
        print(float(a) / float(2000))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(2240))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(28.35))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(28350))
    elif c == 'kg.' or 'kg':
        print(float(a) / float(2.205))
    elif c == 'Mton.' or 'Mton':
        print(float(a) / float(2205))

elif b == 'lb.' or 'lb':
    if c == 'oz.' or 'oz':
        print(float(a) * float(16))
    elif c == 'USton.' or 'USton':
        print(float(a) / float(2000))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(2240))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(453.6))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(453600))
    elif c == 'kg.' or 'kg':
        print(float(a) / float(2.205))
    elif c == 'Mton.' or 'Mton':
        print(float(a) / float(2205))

elif b == 'USton.' or 'USton':
    if c == 'oz.' or 'oz':
        print(float(a) * float(32000))
    elif c == 'lb.' or 'lb':
        print(float(a) * float(2000))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(1.12))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(907200))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(907200000))
    elif c == 'kg.' or 'kg':
        print(float(a) * float(907.2))
    elif c == 'Mton.' or 'Mton':
        print(float(a) / float(1.102))

elif b == 'IMPton.' or 'IMPton':
    if c == 'oz.' or 'oz':
        print(float(a) * float(35840))
    elif c == 'lb.' or 'lb':
        print(float(a) * float(2240))
    elif c == 'USton.' or 'USton':
        print(float(a) * float(1.12))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(1.016e+6))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(1.016e+9))
    elif c == 'kg.' or 'kg':
        print(float(a) * float(1016))
    elif c == 'Mton.' or 'Mton':
        print(float(a) * float(1.016))

elif b == 'gram.' or 'gram':
    if c == 'oz.' or 'oz':
        print(float(a) / float(28.35))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(1000))
    elif c == 'lb.' or 'lb':
        print(float(a) / float(453.6))
    elif c == 'USton.' or 'USton':
        print(float(a) / float(907200))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(1.016e+6))
    elif c == 'kg.' or 'kg':
        print(float(a) / float(1000))
    elif c == 'Mton.' or 'Mton':
        print(float(a) / float(1e+6))

elif b == 'kg.' or 'kg':
    if c == 'oz.' or 'oz':
        print(float(a) * float(35.274))
    elif c == 'lb.' or 'lb':
        print(float(a) * float( 2.205))
    elif c == 'USton.' or 'USton':
        print(float(a) / float(907.2))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(1016))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(1000))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(1000000))
    elif c == 'Mton.' or 'Mton':
        print(float(a) / float(1000))

elif b == 'Mgram.' or 'Mgram':
    if c == 'oz.' or 'oz':
        print(float(a) / float(28350))
    elif c == 'lb.' or 'lb':
        print(float(a) / float(453600))
    elif c == 'USton.' or 'USton':
        print(float(a) / float(9.072e+8))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(1.016e+9))
    elif c == 'gram.' or 'gram':
        print(float(a) * float(1000))
    elif c == 'kg.' or 'kg':
        print(float(a) * float(1e+6))
    elif c == 'Mton.' or 'Mton':
        print(float(a) * float(1e+9))

elif b == 'Mton.' or 'Mton':
    if c == 'oz.' or 'oz':
        print(float(a) * float(35270))
    elif c == 'lb.' or 'lb':
        print(float(a) * float(2205))
    elif c == 'USton.' or 'USton':
        print(float(a) * float(1.102))
    elif c == 'IMPton.' or 'IMPton':
        print(float(a) / float(1.016))
    elif c == 'gram.' or 'gram':
        print(float(a)  * float(1e+6))
    elif c == 'Mgram.' or 'Mgram':
        print(float(a) * float(1e+9))
    elif c == 'kg.' or 'kg':
        print(float(a) * float(1000))

I was expecting that the program actually does what is intended, since I am a beginner I cannot spot the problem nor the solution, so please help!

3

There are 3 answers

1
Oyinlade Demola On

The issue in your code is with the conditions in your if statements. When you write b == 'in.' or 'in', it is not doing what you expect. It should be written as b == 'in.' or b == 'in'. You need to explicitly mention the variable in each condition.

same with all your conditional statement

a = input('Original Amount:')
b = input('Original Unit: ')
c = input('What to convert to: ')

if b == 'in.' or b == 'in':
    if c == 'mm.' or c == 'mm':
        print(float(a) * float(25.4))
    elif c == 'cm.' or c == 'cm':
        print(float(a) * float(2.54))
    # ... (rest of the conditions)
2
vakvakvak On

To make sure that the variables are lowercase, you can write

a = input('Original Amount:').lower()
b = input('Original Unit: ').lower()
c = input('What to convert to: ').lower()

EDIT: As rioV8 commented, you can get rid of the point at the start

a = input('Original Amount:').lower()
b = input('Original Unit: ').lower().strip(".")
c = input('What to convert to: ').lower().strip(".")

Otherwise, then you can check only the first 2 characters, so you do not need to also check for the point character .

if b[:2] == 'in' # also includes b == 'in.'

but you can also check if "in" is inside b

if 'in' in b: # also includes b == 'in.'

you would also save a lot of code if you first convert all the inputs to the same unit, like meters, and then the meters to the c unit.

0
Drywallofcode On

if b == 'in.' or b == 'in

Turns out I had to do this so it would work, if it wasn't for @Oyinlade I would have been none the wiser!