python - Creating if statements with floats -


i trying create conversion program automatically copies converted text windows clipboard. trying make if user inputs number taken out 2 decimal places or less copies converted results taken out 3 places clipboard. if user inputs number taken out 3 decimal places or more copies converted results clipboard taken out 4 decimal places. when run code valueerror cant figure out why. here error getting

line 88, in con   if float_number >= ("%.3f" % float_number): valueerror: incomplete format 

heres part of code thats giving me trouble(and put in comments explain things might missing guys/gals)

def con():     while true:         print("return = main menu, surface = ra conversion")         print(mm_break) #this defined globally elsewhere         number = (input())         if number in('return', 'return'):             break         elif number in('surface', 'surface'):             surf() #i have def surf() elsewhere in program         elif number in('help', 'help'):             help() #i have def help() elsewhere         elif number in('end', 'exit', 'quit')             break         else:              try:                  float(number)              except valueerror:                  print(sys_error) #i have global variable sys_error elsewhere                  break              else:                  float_number = float(number)              convert = float_number/inches              results_3 = ("%.3f" % convert)#converts 3 decimals              results_4 = ("%.4f" % convert)#converts 4 decimals              print(line_break)                  print(" ")              print('\t', results_3)              print('\t', results_4)              print(line_break)              print(" ")              if float_number >= ("%.3f%" % float_number):                  r = tk()                  r.withdraw()                  r.clipboard_clear()                  r.clipboard_append(results_4)#appends results_4 clipboard              else:                  r = tk()                  r.withdraw()                  r.clipboard_clear()                  r.clipboard_append(results_3) 

your error indicates have error in string template, missing %:

if float_number >= (".3f" % float_number): 

should be

if float_number >= ("%.3f" % float_number): 

hovever, trying compare float value string:

if float_number >= ("%.3f" % float_number): 

that throw typeerror:

>>> 0.123 >= '0.123' traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: unorderable types: float() >= str() 

don't that. compare floats floats, use round() create rounded values:

if float_number >= round(float_number, 3): 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -