value = None
print("Insert a value:")
value = input()
if value == None:
print("Any value.!")
else:
print("Your value is different to None: ", value)
Asked
Active
Viewed 139 times
0

James
- 32,991
- 4
- 47
- 70

Eduardo Espinal
- 11
- 1
-
Does this answer your question? [Python None comparison: should I use "is" or ==?](https://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or) – Nick Jun 15 '22 at 07:36
-
`if value is None:` – Nick Jun 15 '22 at 07:36
-
2It won't work anyways, since `input` can't return None. If you don't input anything it will return `''` – Adid Jun 15 '22 at 07:37
-
1@Nick, the return of `input` is always a string. The if-statement will always be False because `value` will never be `None`. – James Jun 15 '22 at 07:39
-
@James yeah, vtc'd only after looking at the comparison test... – Nick Jun 15 '22 at 23:58
1 Answers
4
When you assign value = input()
, value
will now be a string. Even if the user does not provide an input (just hits enter), the value will be an empty string ''
. So comparing to None
will always fail.
Instead, just check if the value is not empty using if value:
print("Insert a value:")
value = input()
if value:
print("Any value.!")
else:
print("Your value is different to None: ", value)

James
- 32,991
- 4
- 47
- 70