0

Here's my code:

ladder = ['mx', 'cx', 'dx', 'x', 'Dx', 'Hx', 'Kx']
ladder_G = ['mg', 'cg', 'dg', 'g', 'Dg', 'Hg', 'Kg']
ladder_L = ['ml', 'cl', 'dl', 'l', 'Dl', 'Hl', 'Kl']
ladder_M = ['mm', 'cm', 'dm', 'm', 'Dm', 'Hm', 'Km']

Number_to_Convert = input("Number: ")
What_unit_is_it = input(f'What Unit is it?{ladder}: ')
Unit = input('To what unit do you want to transform it?' + str(ladder) + ': ')
Number_to_Convert_Float = float(Number_to_Convert)

if Unit == 'mg' or 'cg' or 'dg' or 'g' or 'Dg' or 'Hg' or 'Kg':
    Original_Unit_Grams = ladder_G.index(What_unit_is_it)
    Unit_To_Change_Grams = ladder_G.index(Unit)
    Exponent = abs(int(Original_Unit_Grams) - int(Unit_To_Change_Grams))
    Exponent_String = str(Exponent)

    if What_unit_is_it in ladder_G:
        if Original_Unit_Grams > Unit_To_Change_Grams:
            Index_0 = Exponent_String[0]
            Replace_Exponent = Exponent_String.replace(Index_0, '1')
            Final_Exponent = Replace_Exponent + "0" * Exponent
            Result_Grams = Unit_to_Convert_Float * int(Final_Exponent)
            print(Result_Grams, Unit)
        else:
            Index_0 = Exponent_String[0]
            Replace_Exponent = Exponent_String.replace(Index_0, '1')
            Final_Exponent = Replace_Exponent + "0" * Exponent
            Result_Grams = Unit_to_Convert_Float / int(Final_Exponent)
            print(Result_Grams, Unit)


elif Unit == 'ml' or 'cl' or 'dl' or 'l' or 'Dl' or 'Hl' or 'Kl':
    Original_Unit_Liters = ladder_L.index(What_unit_is_it)
    Unit_To_Change_Liters = ladder_L.index(Unit)
    Exponent = abs(int(Original_Unit_Liters) - int(Unit_To_Change_Liters))
    Exponent_String = str(Exponent)

    if What_unit_is_it in ladder_L:
        if Original_Unit_Liters > Unit_To_Change_Liters:
            Index_0 = Exponent_String[0]
            Replace_Exponent = Exponent_String.replace(Index_0, '1')
            Final_Exponent = Replace_Exponent + "0" * Exponent
            Result_Liters = Number_to_Convert_Float * int(Final_Exponent)
            print(Result_Liters, Unit)
        else:
            Index_0 = Exponent_String[0]
            Replace_Exponent = Exponent_String.replace(Index_0, '1')
            Final_Exponent = Replace_Exponent + "0" * Exponent
            Result_Liters = Number_to_Convert_Float / int(Final_Exponent)
            print(Result_Liters, Unit) 

When I run the code and convert a unit in meters it works just fine, but when trying to convert a unit in liters (liters to kiloliters for example) I get this error

Number: 76
What Unit is it?['mx', 'cx', 'dx', 'x', 'Dx', 'Hx', 'Kx']: Kl
To what unit do you want to transform it?['mx', 'cx', 'dx', 'x', 'Dx', 'Hx', 'Kx']: l
Traceback (most recent call last):
  File "C:/Users/****/PycharmProjects/Convertor/app.fy.py", line 12, in <module>
    Original_Unit_Grams = ladder_G.index(What_unit_is_it)
ValueError: 'Kl' is not in list 

This shouldn't be possible, the first if statement doesn't even accept liter units, only grams. Then why is it running? shouldn't it skip the if statement and check the elif statement? How can I fix this?

Maypher
  • 121
  • 1
  • 2
  • 10
  • 2
    First thing is you probably want to have a read of: https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true (`if Unit == 'mg' or 'cg' or 'dg' or 'g' or 'Dg' or 'Hg' or 'Kg'` isn't doing what you think it does...) – Jon Clements Jan 25 '20 at 17:24

1 Answers1

0

If Unit == 'mg' or 'cg' first checks if unit is equal to mg, and then if cg is not False/None. The second statement is always True, so it will always execute. Instead try:

if Unit in ladder_G:
Nathan
  • 3,558
  • 1
  • 18
  • 38