0
def check(checked): checked = {} if checked == float: return format(checked, '.2f') else: checked = "not a float" return checked # convert to float and check a = input('Enter price for item 1 : ') a = check(a) b = input('Enter price for item 2 : ') c = input('Enter price for item 3 : ') d = input('Enter price for item 4 : ') e = input('Enter price for item 5 : ') print(a) 

whenever I use input for a and expect it to change it returns as not a float even when it has a decimal point. I am trying to get a number to a 2 decimal point limit and if it's not a float value to ignore it. I put the else statement to see what's been going wrong I tried using is instead of == but I still get the same result.

1
  • 1
    Does this answer your question? Determine the type of an object? First of all you reassign your argument by doing checked = {}. Then you are comparing it to the type float. This will always be False. Check the link to see how to determine objects' types Commented Jun 16, 2020 at 15:21

2 Answers 2

0

You’re reassigning the checked variable. Don’t do that.

def check(checked): if isinstance(checked, float): return format(checked, '.2f') else: return "not a float" 

Not sure what you were trying to achieve with checked = {} but all it seemed to be doing was ensuring that checked was always a dictionary, never a float, and never the actual bout value.

To test if something is a float you use isinstance, not == or is

And then reassigning checked to a message which was returned “not a float” is just bad practice.

See the above for a cleaner and (hopefully) working implementation

Sign up to request clarification or add additional context in comments.

Comments

0

In python whatever input you take integer,float or any other ,The Input will be of String data type.

Here is the solution of your problem , It will only work if the input is positive value and the input is either float or integer.

Code:

def check(checked): if checked.isdigit(): return checked else: return format(float(checked), '.2f') a = input('Enter price for item 1 : ') print(check(a)) 

This code will return float value upto 2 decimal places if float value is entered and will leave the number as it is if its not a float value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.