5. Interactive Storytelling
Now that we know how to do branches, we can create a program to do interactive storytelling, or we can create a chatbot. In both cases, we ask the user for the answer to a question, and then we respond with an answer or another question.
As an example, try playing around with Cleverbot. Try typing in things like "What's up?" or "Sorry". Things to think about:
- How "smart" is the computer's responses?
- How could you make it smarter?
- How could you make it feel more like talking to a real person?
Now try out an interactive story, otherwise known as a "choose your own adventure." Here's one called Choice of Robots from the Choice of Games website.
And here's my interactive story on trinket:
Now create your own chatbot or interactive story.
Here are some Python functions that you might find useful. Note that variable can be any Python expression or variable, and str can be any string variable or expression.
One more thing... To test whether a string contains a set of characters, you can use the "in" operator, like this:
- First, create an account over on trinket.io.
- Then click on the "New Trinket" button to create a new Python program.
- Once you've created your game, click on the "Share with others" tab at the bottom of the page, and send the link to me and your friends.
- Get feedback from your friends and family, and then change your program.
- Send me the link again, and tell me what you changed and why you changed it.
- Have fun!
Here are some Python functions that you might find useful. Note that variable can be any Python expression or variable, and str can be any string variable or expression.
- str(variable) - convert a number to a text string
- int(variable) - convert a string integer or decimal to an integer number
- float(variable) - convert a string or integer to a decimal float
- str.capitalize( ) - convert a string to first letter capitalized
- str.endswith(suffix) - return True if string ends with the suffix
- str.find(substring) - return the index of substring, or -1 if not found
- str.isdecimal( ) - return True if all characters are decimals
- str.isnumeric( ) - return True if all characters are numeric
- str.lower( ) - convert a string to all lowercase characters
- str.startswith(prefix) - return True if string starts with the prefix
- str.strip( ) - remove all spaces from the start and end of a string
- str.title( ) - convert a string so each word is capitalized
- str.upper( ) - convert a string to all uppercase characters
One more thing... To test whether a string contains a set of characters, you can use the "in" operator, like this:
if "abc" in mystring:
print("yes")