Python as Interactive Storyteller
The resources on this page were originally created by Dr. Aaron Bradley of Summit Middle School. I've done some reformatting to add clarity but that is about it.
|
During this lesson, you will learn several Python tools:
- How to manipulate text (i.e. strings)
- Assigning and using variables
- Getting information from the user: input()
- Conditional 'if' statements
Lines that start with >>> indicate what you should type into the interpreter at the bottom of the screen. Subsequent lines show the result after you press "Enter." As a reminder, never copy and paste! Programming has elements of playing a musical instrument; the symbols--and the flow of symbols--get ingrained into muscle memory.
You cannot save code you write in the interpreter!
You cannot save code you write in the interpreter!
Strings: Text as Data
1. >>> "Hello Summit!"
'Hello Summit!'
2. >>> "Hello" + "Summit!"
'HelloSummit!'
3. >>> "Hello" + " " + "Summit!"
'Hello Summit!'
4. >>> x = "Hello"
>>> y = "Summit!"
>>> x + " " + y
'Hello Summit!'
>>> y + x
'Summit!Hello'
5. >>> 2 * x
'HelloHello'
>>> 3 * x
'HelloHelloHello'
6. >>> 2 * (x + " ")
'Hello Hello '
1. >>> "Hello Summit!"
'Hello Summit!'
2. >>> "Hello" + "Summit!"
'HelloSummit!'
3. >>> "Hello" + " " + "Summit!"
'Hello Summit!'
4. >>> x = "Hello"
>>> y = "Summit!"
>>> x + " " + y
'Hello Summit!'
>>> y + x
'Summit!Hello'
5. >>> 2 * x
'HelloHello'
>>> 3 * x
'HelloHelloHello'
6. >>> 2 * (x + " ")
'Hello Hello '
Getting Input from the User
1. >>> name = input("What is your name?")
>>>name
'Mr. Busch'
>>> "Hello, " + name + ", it's nice to meet you!"
"Hello, Mr. Busch, it's nice to meet you!"
1. >>> name = input("What is your name?")
>>>name
'Mr. Busch'
>>> "Hello, " + name + ", it's nice to meet you!"
"Hello, Mr. Busch, it's nice to meet you!"
- Delete the default content.
- Write the following code:
age = input("What is your age?")
print("Hello, " + name + ", you are " + age + " years old.")
print("Have a nice day!")
- Press the green arrow ("Go") on the menu bar to run the program.
- What happens?
3. Write a longer quiz. Use print to print a paragraph incorporating the user's answers. Share your quiz with a friend.
4. Save the file as quiz.py in your My Programs folder.
Using Conditionals
Create a new Python module (the top portion of the screen!). Each exercise below refines the previous exercise in some way. Make all changes in the same file. Run the program after each exercise.
1. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
else:
print("That's too bad. Maybe with more experience, you will like it better.")
What happens if you provide an answer other than Y or N? What does if and else do?
2. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
elif answer == "N":
print("That's too bad. Maybe with more experience, you will like it better.")
else:
print("Please answer Y or N in the future.")
Explain how this program corrects the issue with the last program. What does elif do? (elif is short for "else if.
3. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
answer = input("Do you like Python? [Y/N]")
if answer == "Y":
print("Yes, it is a nice language, isn't it?")
elif answer == "N":
answer = input("Which language do you like better?")
print("Oh, you like " + answer + " better? That's interesting.")
else:
print("Y or N!")
elif answer == "N":
print("That's too bad. Maybe with more experience, you will like it better.")
else:
print("Please answer Y or N in the future.")
Notice how indentation indicates the flow of logic. While indentation is used in all languages to help make programs readable, Python actually requires indentation.
Create a new Python module (the top portion of the screen!). Each exercise below refines the previous exercise in some way. Make all changes in the same file. Run the program after each exercise.
1. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
else:
print("That's too bad. Maybe with more experience, you will like it better.")
What happens if you provide an answer other than Y or N? What does if and else do?
2. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
elif answer == "N":
print("That's too bad. Maybe with more experience, you will like it better.")
else:
print("Please answer Y or N in the future.")
Explain how this program corrects the issue with the last program. What does elif do? (elif is short for "else if.
3. answer = input("Do you like programming? [Y/N]")
if answer == "Y":
print("So do I.")
answer = input("Do you like Python? [Y/N]")
if answer == "Y":
print("Yes, it is a nice language, isn't it?")
elif answer == "N":
answer = input("Which language do you like better?")
print("Oh, you like " + answer + " better? That's interesting.")
else:
print("Y or N!")
elif answer == "N":
print("That's too bad. Maybe with more experience, you will like it better.")
else:
print("Please answer Y or N in the future.")
Notice how indentation indicates the flow of logic. While indentation is used in all languages to help make programs readable, Python actually requires indentation.
Choose ONE of the following options:
Create Your Own Chat Bot |
Choose Your Own Adventure |
See if you can pass the Turing Test.
People have been trying to write programs which fool people into thinking they are "chatting" with a real person. Here's a website full of them.
4. Write a conversation program (i.e. a chat bot) about something you want to talk about. -You could talk about going to the movies. -You could talk about your favorite video games. -Travel, etc. |
4. Have you ever read a choose-your-own-adventure story? Programming with conditionals (if/elif/else) allows you to write a computer-based choose-your-own-adventure story. Write one! Share it with a friend.
|