Short Project 4

CPU Category

Though CPUs, and computer hardware in-general, are not the main focus of this course, it can be useful to know a thing or two about computer hardware. The CPU (Central Processing Unit) is generally the piece of hardware that carries out the instructions of the python programs that you run in this class.

In this short project, you will write a program that takes a few input values related to CPU performance. The program should determine whether or not the specified CPU is within one of four categories: high-performance, medium-performance, low-performance, and in need of an upgrade. Name the file cpu.py.

Shown below is an example of the program prompting the user for two inputs, and then printing out the corresponding CPU performance category:

def main():
  gigahertz = 2.7
  core_count = 2
  result = get_result(gigahertz, core_count)
  print(result)
  
main()
That is a low-performance CPU.
  • The first argument, Gigahertz, should be converted to a float
  • The second argument, core count, should be an integer

The program should return one of 4 strings:

  • That is a high-performance CPU.
  • That is a medium-performance CPU.
  • That is a low-performance CPU.
  • That CPU could use an upgrade.

How you determine which to return should be based on the below table:

performance level min GHz min cores
high-performance 3.2 8
medium-performance 2.5 6
low-performance 2.0 2

For example, a medium-performance CPU must satisfy both requirements: a) GHz no less than 2.5 and b) cores no less than 6.

There’s also one “special-case” rule: If a CPU has 20 or more cores, regardless of the other stats, it should be considered high-performance.

Test Cases

Below are examples of program runs. There will be more tests on Gradescope.

def main():
  gigahertz = 2.0
  core_count = 8
  assert get_result(gigahertz, core_count) == "That is a low-performance CPU."
  
  gigahertz = 4.0
  core_count = 7
  assert get_result(gigahertz, core_count) == "That is a medium-performance CPU."
  
  gigahertz = 1.0
  core_count = 10
  assert get_result(gigahertz, core_count) == "That CPU could use an upgrade."
  
  print("End of tests")
  
main()
Before You Begin

This section gives you a quick recap of what we covered in class or introduces any new tips or examples that might help you complete the assignment. Take a few minutes to read through it before you begin.

Logical Expressions

An expression that evaluated to a Boolean value (True or False)

age = 21
age >= 21
True
age > 21
False
age > 35
False

If statements

We can use logical expressions in if-else statements:

if age > 15:
  allowed_to_drive = True
else:
  allowed_to_drive = False
  
print(allowed_to_drive)
True

When the expression age > 15 is evaluated to True lines 2 and 6 run, if it is evaluated to False line to is skipped over and lines 4 and 6 run.

If statements

Add elif statement if you need to check another condition:

if age >= 21:
  allowed_to_drink = True
  allowed_to_drive = True
elif age > 15:
  allowed_to_drink = False
  allowed_to_drive = True
else:
  allowed_to_drink = False
  allowed_to_drive = False
  
print(allowed_to_drink, allowed_to_drive)

When the expression age >= 21 is evaluated to True lines 2, 3 and 11 run, if it is evaluated to False lines 2-3 are skipped over and then age > 15 is evaluated. If age > 15 is evaluated to True lines 5, 6 and 11 run, if it is evaluated to False lines 5-6 are skipped over and lines 8, 9, and 11 run.