Week 7: Python Storytelling

Review

  • Conditions
  • User input

Discussion

  • Mathematical operators
    • Operators can manipulate values (operands)
    • Arithmetic operators
      • +, -, *, **, /, %
    • Comparison (relational) operators
      • ==, !=, >, <, >=, <=
    • Assignment operators
      • ▪ =, +=, -=, *=, /=
Addition+a + b = 10
Subtractionx – y = -5
Multiplication*a * b = 20
Exponent**x3 = 8
Division/b / a = 10
Modulus%b % a = 2
  • Lists
    • A sequence of values, grouped together under a single name
    • Access values by referencing their position in the list
    • i.e.
      • names = [“Jacob”, “Joanna”, “Kathy”]
      • names[1] = “Joanna”

Lesson

Countdown Operators
import time

def count_down():
sec = 10
for count in range(sec): print(sec)
sec -= 1 time.sleep(1)
return
10
9
8
7
6
5
4
3
2
1

Practice

  • Mad Libs: Create a function which prints out a story, filled in with words provided by the user. Ask your classmates to fill in words to complete the story.
Mad libs (Lists)
def mad_libs(words):
print(“Once upon a time in the land of ” + words[0] + “, people became confused. ” + “There were so many ” + words[1] + “, but not enough ” + words[2] + “.”)
return
Enter a place: Candy Land
Enter a thing(plural): coconuts
Enter a thing(plural): beluga whales
Once upon a time in the land of Candy
words = [“”, “”, “”]
categories = [“place”, “thing(plural)”, “thing(plural)”] for count in range(len(categories)):
words[count] = input(“Enter a ” + categories[count])
Land, people became confused. There were so many coconuts, but not enough beluga whales.