How to create a Shakespearean insult generator in python

Download Report

Transcript How to create a Shakespearean insult generator in python

How to maketh a Shakespearean
fig generator in python
the present day we art going to
maketh a program to fig
“
Thou rank milklivered malt-worm
“Thou
Objectives
• Reminder as to random module
• To understand how to use random.int and
random.choice
• To create a dice rolling program
• To extend that program by using if and elif
• To create an shakespearean insult program using
lists,two modules tkinter and random
• To extend the shakespearean insult program by
creating a function
To start with ….
Go to doddle
Save the document called insult list that you have been
sent
Open up python and open up a new scripting area
Select total of 15 Shakespearean insults
Before we go onto creating our insult maker lets remind
ourselves as to how th random module works
Introducing random function
Before students create their Shakespearean insult generator,
they need to become familiar with the random function in
Python.
You are going to write an algorithm to simulate the flipping of
a coin.
Instruct students to type in and run the following code:
Extension task
modify the code to simulate the rolling of a dice i.e.
if the random function returns a 6, print the word
“Six”.
Hint: you might do this by using if and elif to provide choice
Code for random dice
Shakespearean insult generator (simple)
what we are going to do is create a program that
chooses randomly from a range of insults we have
put into lists
We are going to do this by reference to a module
Called random and refer to it using rand.int
Step 1
Type in import random
Then using the list of Shakespearean insults ,create your lists of insults in columns with []
brackets around the listed items
Print “thou”
is old english
for “you”
Referral to random module
saying that choose a random
insult from 1-2 in list 1
Referral to random module
saying that choose a
random insult from 1-2 in
list 2
Referral to random module
saying that choose a random
insult from 1-2 in list 1
The end code should look like this
And will generate a randomly selected insult from each list as above
Now lets take this one step further and create a button that when you click on it generates a
random insult
Extension task using tkinter module
This time we are going to be using a module called tkinter that is inbuilt within python
We still need to put the code in for importing it
This method creates an interface and a button that when you click the button a different
select of insults will appear each time
We are still using the random module but this time we are referring to it using a function
The function uses random.choice to select a random insult in column 1 then 2 then 3
and then display it in the interface
Create an insult giving program
now we are going to use a different module called tkinter
Tkinter is a module that gives you a user interface
What we are going to do is
1.Import random and tkinter modules
2.create three different lists of insults
3.Then we are going to create the user interface using
tkinter
4.Then the space for a button to be placed and named
5. Then using the random module we are going to generate
a random insult from the three lists of insults
1.Import random and tkinter modules
2.create three different lists of Shakespearean insults
From the word document that you have been given on your desks
Choose 6 Shakespearean insults
2 for each column
Then next to column 1- type in the first two insults
then next to column 2 type in the next two insults
then next to column 3 type in the next two insults
So now the code for this section looks something like this
Now we need to create a function
This is the start of the function def pickInsult
The function is defined as pickInsult
Now time to select the insults we want to combine
we start with “thou” , then we add to our first random insult then our second random
insult then our third random insult
3.Then we are going to create the user interface using tkinter
This sets up the user interface
then it names the interface
then it sets a size for the interface
4.Then create the space for a button to be placed and named
This code selects the label name that is being placed on the interface ,says what
font to use and what size
5. Then using the random module we are going to
generate a random insult from the three lists of insults
The end code should look like…
When you run it this is what it looks like