Skip to content

Aug 30: Quiz 0, Formatted Strings

Learning Objectives

After today's class, you should be able to:

  • Create email filters that move unwanted messages into a folder.
  • Explain the logistics for taking a quiz on Canvas and Gradescope.
  • Write an f-string with multiple expressions and format specifiers.

Email Filters

[10 min]

Are you tired of getting emails from Gradescope?

  1. Go to https://dukes.jmu.edu/ (Outlook)
  2. On the left, click Create new folder
    • Note: You might have to click first
    • Name the new folder Gradescope
  3. On the top, click Settings
    • Search for Inbox rules
    • Click Add new rule
      • Name: Gradescope Notifications
      • Condition: From no-reply@gradescope.com
      • Action: Move to Gradescope
      • Check: Rule rule now
  4. Optional: Repeat steps 2 and 3

HW1 Debrief

[10 min]

Some highlights:

  • Inputting an integer: int(input())
  • Escape sequences: \t, \", \\
  • Separate calculations and output

Options at the end of print():

  • sep= separator (default: space)
  • end= ending (default: newline)
print("Today is Monday.")
print("Monday string beans.")

print("Today", "is", "Monday")
print("Today", "is", "Monday", sep="...")

print("Today is Monday, ", end="")
print("Monday string beans.")

print("Today", "is", "Monday", sep="? ", end="!!")
print("Monday string beans.")

Practice Quiz

[15 min]

  • Log in as student (no password)
  • Open Thonny and a web browser
  • Log into Canvas and Gradescope
  • Docstring and PEP 8 not required
  • Direct link on Canvas: Quiz0

F-Strings

[15 min]

Example program from the front page:

welcome.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
"""Display a welcome message for the website.

Author: Chris Mayfield
Version: 08/23/2023
"""

course = input("Enter the course: ")
semester = input("Enter the semester: ")

print(f"Welcome to {course}, {semester}!")

Example format specifiers (after the colon):

numbers.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Example format specifications using f-strings.

Author: Chris Mayfield
Version: 08/30/2023
"""

number = int(input("Please enter an integer: "))

print()
print(f"In decimal (base 10), the number is {number:d}.")
print(f"In binary (base 2), the number is {number:b}.")
print(f"In hexadecimal (base 16), the number is {number:x}.")
print()

number = int(input("Integer between 0 and 99: "))

print()
print(f"Five chars wide, padded with spaces: {number:5d}")
print(f"                  padded with zeros: {number:05d}")
print()

number = int(input("Enter a 7+ digit integer: "))

print()
print(f"With comma separators: {number:,d}")
print()

number = float(input("Enter a floating-point number: "))

print()
print(f"In exponent notation, the number is {number:e}.")
print(f"In fixed-point notation, the number is {number:f}.")
print(f"Rounded to two decimal places: {number:.2f}")
print()

Exercise

Rewrite HW1.4 Bits n' Bytes using an f-string.

Reminders

  • Due Tonight
  • Read Ch 2 – ideally:
    • 1st half before Friday
    • 2nd half before Monday
  • Submit HW 2 – ideally:
    • 1 and 2 before Friday
    • 3 and 4 before Monday