def main():
= 2.7
gigahertz = 2
core_count = get_result(gigahertz, core_count)
result print(result)
main()
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:
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():
= 2.0
gigahertz = 8
core_count assert get_result(gigahertz, core_count) == "That is a low-performance CPU."
= 4.0
gigahertz = 7
core_count assert get_result(gigahertz, core_count) == "That is a medium-performance CPU."
= 1.0
gigahertz = 10
core_count assert get_result(gigahertz, core_count) == "That CPU could use an upgrade."
print("End of tests")
main()
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
)
= 21
age >= 21 age
True
> 21 age
False
> 35 age
False
If statements
We can use logical expressions in if-else
statements:
if age > 15:
= True
allowed_to_drive else:
= False
allowed_to_drive
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:
= True
allowed_to_drink = True
allowed_to_drive elif age > 15:
= False
allowed_to_drink = True
allowed_to_drive else:
= False
allowed_to_drink = False
allowed_to_drive
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.