Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to maketh a Shakespearean fig generator in python

Similar presentations


Presentation on theme: "How to maketh a Shakespearean fig generator in python"— Presentation transcript:

1 How to maketh a Shakespearean fig generator in python
the present day we art going to maketh a program to fig Thou rank milk-livered malt-worm “Thou

2 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

3 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

4 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:

5 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

6 Code for random dice

7 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

8 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

9 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

10 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

11 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

12 1.Import random and tkinter modules

13 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

14 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

15 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

16 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

17 5. Then using the random module we are going to generate a random insult from the three lists of insults

18 The end code should look like…

19 When you run it this is what it looks like


Download ppt "How to maketh a Shakespearean fig generator in python"

Similar presentations


Ads by Google