Search code examples
pythonif-statementstatements

I know this is simple but still these if statements are annoying. when I run it always says length is not defined


Here is my code:

#Starting Point
import time
print("This calculator will convert measurements into a unit of choice")
time.sleep(2)
print("Here are the choice:")
time.sleep(0.5)
print("")
print(" Centimetres, Metres, Kilometres")
time.sleep(0.5)
print("")
print(" Seconds, Minutes, Hours")
time.sleep(0.5)
print("")
print(" Millilitres, litres")
time.sleep(0.5)
print("")
print(" Grams, Kilograms")
time.sleep(0.5)
print("")
#question on convertion
answer = input("Which unit of measurement would you like to use(please enter    L for length T for time V for volume and C for capacity?")


 #program for length
 if answer == "L":
  length = input("What  unit do you want to convert?( C for centimetres M for                      Metres and K for kilometres)")
  print()
 if length  == "C":
    convert_l_1 = input("What do you want to convert centimetres into?(M for    metres and K for Kilometres)")
 elif length == "M":
    convert_l_2 = input("What do you want to convert metres into?( C for    centimetres and K for kilometres)")
 elif length == "K":
    convert_l_3 = input("What do you want to convert Kilometres into?(C for    centimetres and M for metres)")



  #program for Time
   elif answer == "T":
    time = input("What unit do you want to convert?( S for seconds M for    minutes and H for hours)")
     print()
   if time == "S":
     convert_t_1 = ("What do you want to convert seconds into?(M for minutes and   H for hours)")
   elif time == "M":
     convert_t_2 = ("What do you want to convert Minutes to?(S for seconds and H for hours)")
   elif time == "H":
       convert_t_3 = ("What do you want to convert hours to?(S for seconds and M for minutes)")



#program for volume
elif answer == "V":
 volume = input("What unit do you want to convert?(M for millilitres and L for litres)")
print()
if volume == "M":
 convert_v_1 = input("Do you want to convert millilitres to litres?(Y for yes and N for no")
elif volume == "L":
 convert_v_2 = input("Do you want to convert litres to millilitres?(Y for yes and N for no")




#program for capacity
elif answer == "C":
 capacity = input("What unit do you want to convert?( G for grams and K for Kilograms)")

Solution

  • notice the indenting, and the input statements you forgot, also added upper() so people can enter either upper or lower case responses

    #question on convertion
    answer = input("Which unit of measurement would you like to use(please enter L for length T for time V for volume and C for capacity?").upper()
    
    
    #program for length
    if answer == "L":
        length = input("What  unit do you want to convert?( C for centimetres M for Metres and K for kilometres)").upper()
        if length  == "C":
            convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)").upper()
        elif length == "M":
            convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)").upper()
        elif length == "K":
            convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)").upper()
    
    #program for Time
    elif answer == "T":
        time = input("What unit do you want to convert?( S for seconds M for minutes and H for hours)").upper()
        if time == "S":
            convert_t_1 = input("What do you want to convert seconds into?(M for minutes and H for hours)").upper()
        elif time == "M":
            convert_t_2 = input("What do you want to convert Minutes to?(S for seconds and H for hours)").upper()
        elif time == "H":
            convert_t_3 = input("What do you want to convert hours to?(S for seconds and M for minutes)").upper()
    
    #program for volume
    elif answer == "V":
        volume = input("What unit do you want to convert?(M for millilitres and L for litres)").upper()