Shakespearean Insult Generator - Part 2 (Introducing basic File Handling in Python)
How to create a Shakespearean Insult Generator in Python - Part 2 (Free python web tutorial).
Introduction:
In the previous tutorial we explored the use of lists to create a python version of the online “Shakespearean Insult Generator”. This tutorial carries on from the previous one but explores how to improve upon the program by introducing some simple file handling.
Question:
What is wrong with the following list?
In the previous tutorial we explored the use of lists to create a python version of the online “Shakespearean Insult Generator”. This tutorial carries on from the previous one but explores how to improve upon the program by introducing some simple file handling.
Question:
What is wrong with the following list?
column1=["artless", "bawdy", "beslubbering", "bootless", "churlish", "cockered", "clouted", "craven", "currish", "dankish", "dissembling", "droning", "errant", "fawning", "fobbing", "forward", "frothy", "gleeking", "goatish", "gorbellied", "impertinent", "infectious", "jarring", "loggerheaded", "lumpish", "mammering", "mangled", "mewling", "paunchy", "pribbling", "puking", "puny", "qualling", "rank", "reeky", "roguish", "ruttish", "saucy", "spleeny", "spongy", "surly", "tottering", "unmuzzled", "vain", "venomed", "villainous", "warped", "wayward", "weedy", "yeast", "base-court", "bat-fowling", "beef-witted", "beetle-headed", "boil-brained", "clapper-clawed", "clay-brained", "common-kissing", "crook-pated", "dismal-dreaming", "dizzy-eyed", "doghearted", "dread-bolted", "earth-vexing", "elf-skinned", "fat-kidneyed", "fen-sucked", "flap-mouthed", "fly-bitten", "folly-fallen", "fool-born", "full-gorged", "guts-griping", "half-faced", "hasty-witted", "hedge-born", "hell-hated", "idle-headed", "ill-breeding", "ill-nurtured", "knotty-pated", "milk-livered", "motley-minded", "onion-eyed", "plume-plucked", "pottle-deep", "pox-marked", "reeling-ripe", "rough-hewn", "rude-growing", "rump-fed", "shard-borne", "sheep-biting", "spur-galled", "swag-bellied", "tardy-gaited", "tickle-brained", "toad-spotted", "unchin-snouted", "weather-bitten", "apple-john", "baggage", "barnacle", "bladder", "boar-pig", "bugbear", "bum-bailey", "canker-blossom", "clack-dish", "clotpole", "coxcomb", "codpiece", "death-token", "dewberry", "flap-dragon", "flax-wench", "flirt-gill", "foot-licker", "fustilarian", "giglet", "gudgeon", "haggard", "harpy", "hedge-pig", "horn-beast", "hugger-mugger", "joithead", "lewdster", "lout", "maggot-pie", "malt-worm", "mammet", "measle", "minnow", "miscreant", "moldwarp", "mumble-news", "nut-hook", "pigeon-egg", "pignut", "puttock", "pumpion", "ratsbane", "scut", "skainsmate", "strumpet", "varlot", "vassal", "whey-face", "wagtail"]
Answer:
The problem with using lists for this amount of data is that it's easy to make mistakes and difficult to debug.
Thankfully, there is a better way!
The problem with using lists for this amount of data is that it's easy to make mistakes and difficult to debug.
Thankfully, there is a better way!
Activity 1: Introduction to file handling
Before we start improving our “Shakespearean Insult Generator”, let's explore some basic file handling in Python.
WARNING! This section can cause serious damage to your files if you’re not careful!
Let’s re-visit our Minion name generator from the previous tutotial!
Before we start improving our “Shakespearean Insult Generator”, let's explore some basic file handling in Python.
WARNING! This section can cause serious damage to your files if you’re not careful!
Let’s re-visit our Minion name generator from the previous tutotial!
To open a file, you need to make it a variable first. We do this with the following command:
f = open("filename","mode")
Where filename is the name of the file and the mode must be one of the following:
r read only
w write only - NOTE, this destroys any previous version of the file!
r+ read AND write
a append mode (will write only after the existing data)
1. Use the following code window to test your code.
f = open("filename","mode")
Where filename is the name of the file and the mode must be one of the following:
r read only
w write only - NOTE, this destroys any previous version of the file!
r+ read AND write
a append mode (will write only after the existing data)
1. Use the following code window to test your code.
2. Type in and run the following code in the code window above:
#
file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # print(line) # read the next line line = file.readline() # file.close() |
Challenge:
Now that you know how to open a simple text file and print the contents, Let's explore how to store each line of the file in a list so as to manipulate the data later.
3. Add the following line at the beginning of your code:
names=[ ]
This new line will create an empty list to store our minion mames.
4. Next, replace the line print(line) with the following code:
names.append(line)
This new code will add the contents of each line of the text file to the newly created list, called names.
Your code should look like this:
- Fill in the blank comments (#), explaining what the code is doing.
Now that you know how to open a simple text file and print the contents, Let's explore how to store each line of the file in a list so as to manipulate the data later.
3. Add the following line at the beginning of your code:
names=[ ]
This new line will create an empty list to store our minion mames.
4. Next, replace the line print(line) with the following code:
names.append(line)
This new code will add the contents of each line of the text file to the newly created list, called names.
Your code should look like this:
names=[]
# open the file and start searching file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # names.append(line) # read the next line line = file.readline() # close the file file.close() |
5. Finally, let's add the code to pick a name at random.
Add the following code onto the end of your script:
print("Minion" + " " + random.choice(names))
Question:
What needs to be imported in order for the 'random' function to work?
Answer:
Add the line: import random at the beginning of your script.
Run your code. Your finished code should look like this:
Add the following code onto the end of your script:
print("Minion" + " " + random.choice(names))
Question:
What needs to be imported in order for the 'random' function to work?
Answer:
Add the line: import random at the beginning of your script.
Run your code. Your finished code should look like this:
import random
names=[] # open the file and start searching file = open("minions.txt","r") # read the first line line = file.readline() # repeat as long as the line is not empty while line != "": # names.append(line) # read the next line line = file.readline() # close the file file.close() print("Minion" + " " + random.choice(names)) |
Activity 2: Improving the Insult generator
Challenge:
Using what you have learnt and using the resources at your disposal, update their Shakespearean Insult generator from the previous tutorial so that it reads the list of random insults from a file.
Hint: You will need to have 3 separate files (One for each column) and 3 empty lists.
Challenge:
Using what you have learnt and using the resources at your disposal, update their Shakespearean Insult generator from the previous tutorial so that it reads the list of random insults from a file.
Hint: You will need to have 3 separate files (One for each column) and 3 empty lists.
column1.txt |
column2.txt |
column3.txt |
Example Solutions
insult_creator_v1_txt.py |
insult_creator_v1_txt_tkinter.py |
Unless otherwise specified, everything in this repository is covered by the following licence:
The Shakespearean Insult Generator is licenced under a Creative Commons Attribution 4.0 International License.
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Based on the Shakespearean Insulter: http://www.pangloss.com/seidel/Shaker/
Tags: programming language python, python programming language, python coding language, programming languages python, python code language, python programming language example, what is the python programming language, python scripting, python language tutorial, what type of programming language is Python, python class, python how to learn, learn python, learn python from scratch, where to learn Python, coding on computer, computer coding, best place to learn coding online, online coding, online code, computer programming and Coding, hour of code program, computer science degree programs, programs for computer science, what does coding mean in computers, how does computer code work, what is computer programmer.