【问题标题】:Why is my Python code reading things in an if else statement that it's not supposed to?为什么我的 Python 代码会在 if else 语句中读取它不应该读取的内容?
【发布时间】:2023-01-29 01:53:21
【问题描述】:

出于某种原因,我的代码执行了不应该运行的东西,塞进了 if ... else 语句中。

if platform == "win32":
  if str(platform.release()) == "10" or str(platform.release()) == "11":
    print(f"{bcolors.WARNING}Welcome to {bcolors.BOLD}B Kernel{bcolors.ENDC}")
  else:
    print("Welcome to B Kernel.")
else:
  print(f"{bcolors.WARNING}Welcome to {bcolors.BOLD}B Kernel{bcolors.ENDC}")

return True

它运行该 sn-p 的第三行,即使我在 Windows 7 上使用它。它应该运行第 5 行,但没有。

enter image description here

我将第一行从“Windows”更改为“win32”,怀疑错误来自第 7 行,但它来自第三行,结果也是同样的错误。

【问题讨论】:

  • platform 是什么,您可以直接将它与字符串进行比较,还可以调用 release() 吗?
  • 您如何打印您正在检查的值的值,看看它们是否符合您的预期?
  • platform 获取操作系统类型(Windows、macOS、Linux),而“release”是操作系统版本(XP、7、10、11)
  • 是的,但你没有调用任何东西,你只是将包与字符串进行比较

标签: python windows if-statement


【解决方案1】:

它没有运行第 3 行。它正在运行第 7 个,与第 3 个相同。 platform不等于"win32"platform 很可能是 platform module,它不等于字符串。

如果platform曾是"win32",那么当你试图调用platform.release()时,你会得到一个异常。

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多