hello,
when I was writing length converter I came across this problem
while statement(bold one) gives me Indentation Error that says "unindent does not match any outer indentation level" and I was wondering why's that? Could anyone suggest me how to fix it

def leng():
     def milimetrai():
        while True:
            try:
                x = float(raw_input("Enter lenght it mm or cm"))
                break
            except ValueError:
                print "Wrong Input" , x
        mm = x*10
        cm = x/10
        print x,'mm =',cm,'cm'    #centimetrai
        print x,'cm =',mm,'mm'           #milimetrai
     [B]while True:[/B]
        deci = raw_input("To convert mm to cm or cm to mm press 1")
        if deci == '1':
            milimetrai()
        else:
            exit()

You need to indent the statement block that belongs to the second while loop.

Note:
You can write a function within a function in Python, but generally it is only used for special cases like closures.

You need to indent the statement block that belongs to the second while loop.

Note:
You can write a function within a function in Python, but generally it is only used for special cases like closures.

It didn't work. It still say IndendationError

There are several problem with your code.
Function within a function you dont use it in code like this as vegaseat poinet out.
You break out to nothing.
This line will not work print "Wrong Input , x" in exception handling because off x

I rewrite to show a better direction to go.
This way it print result,and then main menu come again.

def mm():    
    while True:        
        try:                    
            x = float(raw_input("Enter lenght in cm: "))
            mm = x * 10                           
            print '%s is %.3fmm\n' % (x,mm)
            break
        except ValueError:            
            print "Wrong Input,try again"

def cm():    
    while True:        
        try:                    
            x = float(raw_input("Enter lenght in mm: "))
            cm = x / 10                           
            print '%s is %.3fcm\n' % (x,cm)
            break
        except ValueError:            
            print "Wrong Input,try again" 

def main():   
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Convert cm -> mm'
        print '(2) Convert mm -> cm' 
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':            
            mm()            
        elif choice == '2':
            cm()           
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice   

if __name__ == '__main__':
    main()

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There are several problem with your code.
Function within a function you dont use it in code like this as vegaseat poinet out.
You break out to nothing.
This line will not work print "Wrong Input , x" in exception handling because off x

I rewrite to show a better direction to go.
This way it print result,and then main menu come again.

def mm():    
    while True:        
        try:                    
            x = float(raw_input("Enter lenght in cm: "))
            mm = x * 10                           
            print '%s is %.3fmm\n' % (x,mm)
            break
        except ValueError:            
            print "Wrong Input,try again"

def cm():    
    while True:        
        try:                    
            x = float(raw_input("Enter lenght in mm: "))
            cm = x / 10                           
            print '%s is %.3fcm\n' % (x,cm)
            break
        except ValueError:            
            print "Wrong Input,try again" 

def main():   
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Convert cm -> mm'
        print '(2) Convert mm -> cm' 
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':            
            mm()            
        elif choice == '2':
            cm()           
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice   

if __name__ == '__main__':
    main()

end quote.

Could you explain me this part print '%s is %.3fmm\n' % (x,mm)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Dokino> Could you explain me this part print '%s is %.3fmm\n' % (x,mm) print accepts a string with operants to be formatted. '%s is %.3fmm\n' The first two % gets substituted by the variables x and mm respectively. %s tell print that it should be formatted as a string, %.3f would be formatted as a float with three digits after the decimal.
% (x,mm) is the way print knows where to find the variables to use for the format.

Edited 6 Years Ago by Aia: n/a

Dokino> Could you explain me this part print '%s is %.3fmm\n' % (x,mm) print accepts a string with operants to be formatted. '%s is %.3fmm\n' The first two % gets substituted by the variables x and mm respectively. %s tell print that it should be formatted as a string, %.3f would be formatted as a float with three digits after the decimal.
% (x,mm) is the way print knows where to find the variables to use for the format.

mm it's that simple ;) thanks

This article has been dead for over six months. Start a new discussion instead.