【问题标题】:How to check if a variable is NOT equal to a string [duplicate]如何检查变量是否不等于字符串[重复]
【发布时间】:2021-12-11 21:25:40
【问题描述】:

我正在尝试检查 osys 变量是否不等于 'Linux'。我机器上 platform.system() 的输出是'Linux'。然后将其分配给osys 的变量。

def getos():
osys=platform.system()
if osys is not "'Linux'":
    print(color.lightred + "This program only runs on Linux operating systems.")
    time.sleep(2)
    quit()
getos()

我正在使用此代码检查osys 是否为'Linux',如果不是,则程序将关闭,因为该程序仅适用于 linux。相反,当我运行此代码时,我总是得到This program only runs on Linux operating systems 的输出字符串,而不是继续运行代码。有谁知道如何解决这个问题。

【问题讨论】:

  • if osys is not "Linux":...怎么样?目前尚不清楚为什么您选择在此处包含单引号 inside 您的字符串。投票结束是一个印刷错误。
  • @esqew 我想你的意思是!= "Linux"?
  • 比较文字/字符串时应使用!= 运算符,而不是is。这应该引发SyntaxWarning
  • @OneCricketeer 好的,谢谢 - 今天早上喝完第三杯咖啡后动作太快了。 if osys != "Linux":

标签: python


【解决方案1】:

试试这个,你不需要引号内的引号。

if osys != "Linux":

【讨论】:

    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 2015-02-04
    • 2018-10-06
    • 1970-01-01
    • 2012-09-28
    • 2012-10-16
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多