程序总是返回tru

2024-09-29 23:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个代码,要求用户输入千兆赫兹、内核,并询问是否存在超线程,然后根据下面的图表打印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.")

我所有的其他结果都是有效的,只有当输入是像giga=2.8 core\u count=6 hyper=false这样的东西时

它应该打印“中等性能的cpu”,但它可以识别真实的cpu并打印高性能的cpu


Tags: orandcoreifiscountperformancecpu
1条回答
网友
1楼 · 发布于 2024-09-29 23:24:43

让我们看看您编写了什么代码,以及解释程序为什么要这么做。为了清楚起见,我将使用括号,但它们不是严格必要的。首先,你已经写好了

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

Python将这行解释为

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

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

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

相反,你可以扩大你的条件:

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

或者您可以节省一些重复,并使用字符串具有的内置^{}函数:

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

相关问题 更多 >

    热门问题