Inhale.Py
Inhale.Py

Reputation: 260

Creating if statements with floats

I am trying to create a conversion program that automatically copies converted text to windows clipboard. I am trying to make it so that if the user inputs a number that is taken out to 2 decimal places or less it copies the converted results taken out to 3 places to the clipboard. If the user inputs a number that is taken out to 3 decimal places or more it copies the converted results to the clipboard taken out to 4 decimal places. When I run the code I get a ValueError but I cant figure out why. Here is the error I am getting

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

Heres the part of the code thats giving me trouble(and I put in comments to explain things that might look like they are missing for you guys/gals)

def con():
    While True:
        print("Return = Main Menu, Surface = RA Conversion")
        print(MM_break) #This is defined globally elsewhere
        number = (input())
        if number in('Return', 'return'):
            break
        elif number in('Surface', 'surface'):
            surf() #I have a def surf() elsewhere in the program
        elif number in('help', 'Help'):
            help() #I have a def for help() elsewhere
        elif number in('end', 'exit', 'quit')
            break
        else:
             try:
                 float(number)
             except ValueError:
                 print(sys_Error) #I have a global variable for 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 to 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 to clipboard
             else:
                 r = Tk()
                 r.withdraw()
                 r.clipboard_clear()
                 r.clipboard_append(Results_3)

Upvotes: 2

Views: 5749

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122092

Your error indicates you have an error in the string template, you are missing a %:

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

should be

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

Hovever, now you are trying to compare a float value with a string:

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

That will throw a TypeError:

>>> 0.123 >= '0.123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: float() >= str()

Don't do that. Compare floats to floats, use round() to create rounded values:

if float_number >= round(float_number, 3):

Upvotes: 4

Related Questions