Python Programming · Week 4
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
The same steps as last week.
This video is all typing. Pause whenever you need to catch up.
One line, Enter, read the answer. Then the next one.
>>> n = 7
>>> n > 3
>>> n % 2 == 0
>>> n != 7
>>> "Kim" == "kim"
>>> "10" < "9"
>>> n > 3 and n < 5
>>> 3 < n < 5
>>> n > 3 or n < 5
then look back
Which answers were False?
n % 2 == 0 is True when n is even · 7 % 2 is 1, so False · example 1 uses this
In your week04 folder: the fourteen lines from the theory video.
score = int(input("Score: "))
if 0 <= score <= 100:
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
else:
print("A score is from 0 to 100.") Score: 95
Grade: A
Score: 59
Grade: F
Score: 150
A score is from 0 to 100.
run again100 · 60 · 0 · -5
On purpose. After each one, undo it: ⌘Z (Windows: Ctrl+Z).
at the prompt
if x = 5:
grade.py, line 4
delete 4 of the 8 spaces before print("Grade: A")
grade.py
move the two D lines up, right under print("Grade: A") · type 85
grade.py, line 3
change score >= 90 to score > 90 · type 90
two loud errors: good news · two silent wrong answers: the worst case
Ask for a whole number. Print whether it is even or odd.
even: dividing by 2 leaves nothing
on the screen
Number: 7
7 is odd
Number: 10
10 is even
the code
n = int(input("Number: "))
if n % 2 == 0:
print(f"{n} is even")
else:
print(f"{n} is odd") Ask for a resistor's nominal value (the value printed on it) and its measured value, in ohms. Within 5% of the nominal value: OK; otherwise: Out of tolerance. Either way, print how many ohms off, with one digit after the point.
on the screen
Nominal (ohms): 220
Measured (ohms): 216.5
OK: 3.5 ohms off
Nominal (ohms): 220
Measured (ohms): 235
Out of tolerance: 15.0 ohms off
the code
nominal = float(input("Nominal (ohms): "))
measured = float(input("Measured (ohms): "))
diff = abs(measured - nominal)
if diff <= nominal * 0.05:
print(f"OK: {diff:.1f} ohms off")
else:
print(f"Out of tolerance: {diff:.1f} ohms off") Ask for a temperature in Celsius, then F or K, in a small or a capital letter. Print it in Fahrenheit with one digit after the point, or in Kelvin with two. Anything else: Please type F or K.
F = C × 9 / 5 + 32 · K = C + 273.15
on the screen
Celsius: 36.5
F or K? f
97.7 F
Celsius: 36.5
F or K? K
309.65 K
Celsius: 36.5
F or K? x
Please type F or K.
the code
c = float(input("Celsius: "))
unit = input("F or K? ")
if unit == "F" or unit == "f":
print(f"{c * 9 / 5 + 32:.1f} F")
elif unit == "K" or unit == "k":
print(f"{c + 273.15:.2f} K")
else:
print("Please type F or K.") Ask for a password, then ask for it again. Different: They do not match. The same but shorter than 8 characters: Too short. Otherwise: Password set.
on the screen · one run
Password: python314
Type it again: python314
Password set.
the values after the prompts are typed · the prompt words are up to you
examples · typed values in order
typed
prints
python314, python314
Password set.
python314, Python314
They do not match.
abc, abc
Too short.
Ask for a whole number. Is it a multiple of 3, of 5, of both, or of neither? 0 or less: Please type a number greater than 0.
on the screen · one run
Number: 30
Multiple of 3 and 5
the values after the prompts are typed · the prompt words are up to you
examples · typed values in order
typed
prints
30
Multiple of 3 and 5
9
Multiple of 3
10
Multiple of 5
7
Neither
-3
Please type a number greater than 0.
Ask for a gate (AND, OR, or XOR, typed in capital letters), then inputs A and B, each 0 or 1. Print the output. AND: 1 only when both are 1. OR: 1 when at least one is 1. XOR: 1 when exactly one is 1. Any other gate: Unknown gate.
on the screen · one run
Gate: XOR
A: 1
B: 0
Output: 1
the values after the prompts are typed · the prompt words are up to you
examples · typed values in order
typed
prints
AND, 1, 1
Output: 1
OR, 0, 0
Output: 0
XOR, 1, 0
Output: 1
NAND, 1, 1
Unknown gate.
Ask for a year. Print whether it is a leap year: divisible by 4 but not by 100, or divisible by 400.
on the screen · one run
Year: 2024
2024 is a leap year.
the values after the prompts are typed · the prompt words are up to you
examples · typed values in order
typed
prints
2024
2024 is a leap year.
1900
1900 is not a leap year.
2000
2000 is a leap year.
2026
2026 is not a leap year.
Next steps
Before class. Everything you need is on e-Class.
See you in class.