gpt4 book ai didi

python - 程序总是返回真

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:37 26 4
gpt4 key购买 nike

我需要创建一个代码,要求用户输入千兆赫兹、核心数,并询问是否有超线程,然后根据下表打印 cpu 的性能(高/中/低)。我知道这是因为字符串在 Python 中是真实的,但我已经尝试了所有我能找到的修复它的建议!

enter image description here

giga = float(input("Enter CPU gigahertz:\n"))
core_count = int(input("Enter CPU core count:\n"))
hyper = input("Enter CPU hyperthreading (True or False):\n")

if hyper == "true" or "True":
if giga >= 1.9 and giga < 2.4:
if 4>core_count>=2:
print("\nThat is a low-performance CPU.")

elif giga >= 2.4 and giga < 2.7:
if core_count>=4 and core_count <6:
print("\nThat is a medium-performance CPU.")
elif 4>core_count>=2:
print("\nThat is a low-performance CPU.")

elif giga >= 2.7:
if core_count>=4 and core_count <6:
print("\nThat is a medium-performance CPU.")
elif core_count>=2 and core_count < 4:
print("\nThat is a low-performance CPU.")
elif core_count >= 6:
print("\nThat is a high-performance CPU.")

elif giga < 1.9 or core_count < 2:
print("\nThat CPU could use an upgrade.")

if core_count>=20:
print("\nThat is a high-performance CPU.")

elif hyper == "False":
if giga >= 2.4 and giga < 2.8:
if core_count >= 2 and core_count < 6:
print("\nThat is a low-performance CPU.")

elif giga >= 2.8 and giga < 3.2:
if core_count >= 6 and core_count < 8:
print("\nThat is a medium-performance CPU.")
if core_count <6:
print("\nThat is a low-performance CPU.")

elif giga >= 3.2:
if core_count >= 8:
print("\nThat is a high-performance CPU.")
if core_count >= 6 and core_count < 8:
print("\nThat is a medium-performance CPU.")
if core_count <6:
print("\nThat is a low-performance CPU.")


elif giga < 2.4 or core_count < 2:
print("\nThat CPU could use an upgrade.")

我的所有其他结果只有在输入为 #like giga = 2.8 core_count = 6 hyper = false 时才会起作用

它应该打印“medium-performance cpu”但它识别为 true 并打印 high-performance

最佳答案

让我们看一下您编写的代码以及解释器为何执行它正在执行的操作。为了清楚起见,我将使用括号,但它们并不是绝对必要的。首先,你已经写了。

if hyper == "true" or "True":

Python 将这一行解释为

if ((hyper == "true") or ("True")):

因为“True”为 True (all non-empty sequences are True),并且 or 运算符在其旁边的任一语句为真时将始终返回 True,因此此 if 语句将即使 hyper 为 False,也始终被视为 True:

if ((False) or (True)): # This will evaluate to True

相反,您可以扩展您的条件:

if (hyper == "true") or (hyper == "True"):

或者您可以避免重复并使用内置的 lower字符串具有的功能:

if (hyper.lower() = "true"):

关于python - 程序总是返回真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com