简体   繁体   English

在“if”语句中尝试 Python 的“None”,但我无法打印消息:“print(“Any value.!”)”将 None 与空值进行比较,为什么我不能?

[英]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.

相关问题 为什么我不能在 1 行中使用 python 打印句子和变量值? - why i cant print sentence and variable value in 1 line use python? 我无法打印禁用文本框内的值 = Python selenium - I cant print the value inside of the disabled textbox = Python selenium 为什么我得到“无”作为打印语句的输出? - Why do I get 'None' as the output from a print statement? 为什么在我编写然后在执行简单计算的 python 中使用 if 语句打印时没有显示任何内容? - Why do I get none displayed when I write and then print using if statement in python that does simple computation? 如果python中的参数为None,如何打印默认值 - how to print the default value if argument is None in python python打印语句中的意外没有 - Unexpected None in python print statement get_text()返回空值。 我想将其转换为无 - get_text() return empty value. I want to convert it to None 为什么我会收到完整的“无”印刷品? - why am i receiving a print full of 'None'? 为什么在尝试使用 python 中的 BeautifulSoup 刮取任何数据时,我一直没有得到任何列表或空列表 - why i keep getting none or empty lists when trying to scrape any data with BeautifulSoup in python 我如何更正 for 循环中的错误(它说 print func 不能返回任何值)? - How can i correct the error inside for loop(it says print func cant return any value)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM