我需要创建一个代码,要求用户输入千兆赫兹、内核,并询问是否存在超线程,然后根据下面的图表打印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.")我的所有其他结果都是有效的,只有当输入是#如giga =2.8hyper=6hyper= false时才有效
它应该打印"medium-performance cpu“,但它可以识别true并打印高性能
发布于 2019-09-15 10:12:34
让我们来看看你编写了什么代码,以及为什么解释器要做它正在做的事情。为了清楚起见,我将使用括号,但它们并不是绝对必要的。首先,你已经写好了。
if hyper == "true" or "True":Python将此行解释为
if ((hyper == "true") or ("True")):由于" True“为True (all non-empty sequences are True),并且如果其旁边的任一语句为true,则or运算符将始终返回True,因此即使if为False,也将始终将此if语句视为True:
if ((False) or (True)): # This will evaluate to True相反,您可以扩展您的条件:
if (hyper == "true") or (hyper == "True"):或者,您可以省去一些重复操作,而使用字符串所具有的内置lower函数:
if (hyper.lower() = "true"):https://stackoverflow.com/questions/57940319
复制相似问题