1

I need to create a code that asks for the user to input gigahertz, cores, and ask if there is hyper-threading, then prints the performance of the cpu (high/med/low) based on the chart below. I know it is something with strings being truthy in Python but I have tried every suggestion I can find to fix it!

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.")

all of my other outcomes work it is only when the inputs are something # like giga = 2.8 core_count = 6 hyper = false

it should print "medium-performance cpu" but it recognizes true and prints high-performance

monica
  • 31
  • 4
  • Welcome to Stackoverflow, please read [How To Ask](https://stackoverflow.com/help/how-to-ask). Pay special attention to [How To Create MCVE](https://stackoverflow.com/help/mcve). Make sure you tag your question with proper labels (programming language, relevant technologies etc). The more effort you'll put into posting a good question: one which is easy to read, understand and which is [on topic](https://stackoverflow.com/help/on-topic) - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! – Nir Alfasi Sep 15 '19 at 00:51
  • 2
    I retracted my close vote because it's unclear if that's your problem, but see [here](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value). `if hyper == "true" or "True"` doesn't work as you're intending. And, once you get this code working, you can post it on Code Review to get improvement suggestions. For example, `if core_count>=4 and core_count <6` can be written as `if 4 <= core_count < 6` – Carcigenicate Sep 15 '19 at 00:52
  • 2
    You also need to indent the print statements under `core_count>=20` – OneCricketeer Sep 15 '19 at 01:04

1 Answers1

1

Let's have a look at what you've coded and why the interpreter is doing what it's doing. I'm going to use parentheses for clarity, but they're not strictly necessary. First of all, you've written.

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

Python interprets this line as

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

Since "True" is True (all non-empty sequences are True), and the or operator will always return True if either of statements beside it are true, this if statement will always be considered True even if hyper is False:

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

Instead you could expand your conditions:

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

Or you could save yourself some repetition and use the built-in lower function that strings have:

if (hyper.lower() = "true"):
Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43