[英]Trying Python's "None" in a "if" statement, but i cant print the message: "print("Any value.!")" comparing None with an empty Value, why i cant?
value = None
print("Insert a value:")
value = input()
if value == None:
print("Any value.!")
else:
print("Your value is different to None: ", value)
When you assign value = input()
, value
will now be a string.当您分配value = input()
时, value
现在将是一个字符串。 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.所以与None
比较总是会失败。
Instead, just check if the value is not empty using if value:
相反,只需使用if value:
print("Insert a value:")
value = input()
if value:
print("Any value.!")
else:
print("Your value is different to None: ", value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.