Python Programming · Week 4

Comparisons and Conditionals:
Practice

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Setup

Before we start

The same steps as last week.

  1. 1 VS Code → open your python_course folder
  2. 2 New Folder → week04
  3. 3 Terminal → python3 → the three arrows

This video is all typing. Pause whenever you need to catch up.

Drill

At the prompt: type these

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 == 0n != 7"Kim" == "kim"n > 3 and n < 53 < n < 5

n % 2 == 0 is True when n is even · 7 % 2 is 1, so False · example 1 uses this

Files

Now a file: grade.py

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

Drill

Break four things

On purpose. After each one, undo it: ⌘Z (Windows: Ctrl+Z).

at the prompt

if x = 5:

SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? loud

grade.py, line 4

delete 4 of the 8 spaces before print("Grade: A")

IndentationError: expected an indented block after 'if' statement on line 3 loud

grade.py

move the two D lines up, right under print("Grade: A") · type 85

Grade: D silent

grade.py, line 3

change score >= 90 to score > 90 · type 90

Grade: B silent

two loud errors: good news · two silent wrong answers: the worst case

Example · 1

Even or odd

even.py

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")
Example · 2

A tolerance checker

tolerance.py

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")
Example · 3

Celsius to F or K

convert.py

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.")
Exercise 3 · Problem 1

Password check

week04_problem1.py 2 points

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.

Exercise 3 · Problem 2

Multiples of 3 and 5

week04_problem2.py 2 points

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.

Exercise 3 · Problem 3

A logic gate

week04_problem3.py 2 points

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.

Exercise 3 · Problem 4

Leap year

week04_problem4.py 4 points

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.

Exercise 3

How to submit

  1. 1 download week04_exercise.zip from e-Class · unzip it: four files, one per problem
  2. 2 put the four files in python_course/week04 · open them in VS Code
  3. 3 in each file: write the code · save · run it and try every example
  4. 4 zip the four files · upload the zip on e-Class · the deadline is in the post
10 points: 2 · 2 · 2 · 4attempted: full points, right or wronglate: half the pointssolutions: only in classAI: allowed, but try alone first

Next steps

Now: exercise 3.

Before class. Everything you need is on e-Class.

See you in class.

← All weeks
01 / 00
Scroll · ↓ · Space