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