number = int or float (input("enter a nimber: "))
if type(number) == float :
print ("that's float")
elif type(number) == str :
print ("that's str")
else:
if number % 2 == 0:
print("even int")
else:
print("odd int")
Asked
Active
Viewed 66 times
-1
-
See https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-represents-a-number-float-or-int – jarmod Aug 07 '23 at 18:53
-
2@HarunYilmaz "not to be used when defining variables" it *can* be, you just have to understand the semantics. – juanpa.arrivillaga Aug 07 '23 at 18:54
-
1`input` always returns a string, if you want something else you need to convert it. – Mark Ransom Aug 07 '23 at 18:55
-
You’ll want a different approach. I suggest getting the input as a string first and then using multiple `try` blocks (or a loop) to try converting it to an int and then a float. – Samwise Aug 07 '23 at 19:07
-
ChatGPT has a perfectly nice explanation for this. Unfortunately I shall not post generated content. Maybe you try it yourself. – Thomas Weller Aug 07 '23 at 19:11
1 Answers
0
or
operator resolves to first truthy value, see the docs for details. You can check if value is truthy by converting it to boolean: bool(int)
→ True
and bool(float)
→ True
. Thus, as both int
and float
builtins are truthy, the or
operator gives you which ever was listed first. In your case int or float
always resolves to int
:
>>> int or float
<class 'int'>

kangasta
- 642
- 7
- 17
-
While this is not wrong, I doubt that it helps OP achieve their goal. – Thomas Weller Aug 07 '23 at 19:08
-
If the goal is to understand why their code always returns integer, it might. If the goal is to get the code to working state, then the linked post has better explanation. – kangasta Aug 07 '23 at 19:11