Python Poetry

advertisement
An introduction to Python
A series of introductory lessons to
Python that does not use too much
maths!
Write a poem
Lets write a four line poem
Challenge 1
print("Mashed potatoes on the ceiling.")
print("Green beans on the floor.")
print("Stewed tomatoes in the corner.")
print("Squash upon the door.")
Change a vegetable
We are now going to change the program so
that we can change the name of one of the
vegetables
To do this we need to:
• Add a variable
• Make sure that it appears in a line
Challenge 2
veg=“Carrots”
print("Mashed “,veg,” on the ceiling.")
print("Green beans on the floor.")
print("Stewed tomatoes in the corner.")
print("Squash upon the door.")
Challenge 3
Can you change the program so that the variable
appears in every line?
# for example
print("Green",veg,"on the floor.")
print(veg,"on the door.")
Challenge 3
veg="Carrots"
print("Mashed ",veg, “ on the ceiling.")
print("Green “,veg, “ on the floor.")
print("Stewed “,veg, “ in the corner.")
print(veg, “ upon the door.")
Make the computer input
We are now going to change the program so
that the computer accepts an input from the
keyboard
Challenge 4
veg=input(“type in the name of a vegetable “)
print("Mashed",veg, "on the ceiling.")
print("Green",veg,"on the floor.")
print("Stewed",veg,"in the corner.")
print(veg,"upon the door.")
Have four different vegetable
Instead of having just one input can you change
the program so that it has four – one for each
line.
Challenge 5
Change here
Change here
Change here
Change here
print("Mashed", Change here, "on the ceiling.")
print("Green", Change here,"on the floor.")
print("Stewed", Change here,"in the corner.")
print(Change here,"upon the door.")
Challenge 5
veg1=input(“What is vegetable 1? ”)
veg2=input(“What is vegetable 2? “)
veg3=input(“What is vegetable 3? “)
veg4=input(“What is vegetable 4? “)
print("Mashed", veg1, "on the ceiling.")
print("Green", veg2,"on the floor.")
print("Stewed", veg3,"in the corner.")
print(veg4,"upon the door.")
Random vegetable
Now lets change the program so that random
vegetables are displayed
To do this we need to:
• Import the commands for the random library
• Create a list with vegetable names
• Choose a vegetable
First part of the program
# To use random numbers
import random
veglist=[“beans”,”brocolli”,”cabbage”,”cauliflower”]
veg=random.choice(veglist)
Challenge 6
#to use random numbers
import random
veglist=[“Beans”,“Broccoli”,”Cabbage”]
veg=random.choice(veglist)
print("Mashed",veg, "on the ceiling.")
print("Green",veg,"on the floor.")
print("Stewed",veg,"in the corner.")
print(veg,"upon the door.")
Challenge 7
Create a program that randomly chooses a member
of your class
• Create a new list with names of you class
• Randomly choose a name
• Print the chosen name
Challenge 8
Create a program to roll a six sided dice
Challenge 9
Let’s create a quiz program
Challenge 9
correct = 0
print("Here is a quiz to test your knowledge of television")
print()
print("Question 1")
print(“What type of animal is Shaun?")
print()
print(“a. Cow")
print(“b. Dog")
print("c. Sheep”)
correct = “c”
answer = input("Make your choice: ")
if answer == correct:
print(“Right Answer!")
else:
print(“Wrong Answer!”)
Challenge 9
correct = 0
print("Here is a quiz to test your knowledge of television")
print()
print("Question 1")
print(“What type of animal is Shaun?")
print()
print(“a. Cow")
print(“b. Dog")
print("c. Sheep”)
correct = “c”
answer = input("Make your choice: ")
if answer == correct:
print(“Right Answer")
Try to think of a reason for the two equal signs
else:
It is called a logic test
print(“Wrong Answer!”)
Challenge 10
Can you add two more questions?
Challenge 11
We would like to add a score to the quiz
To do this we need to:
• create a variable – score
• If correct we will add one to the score
• We will also print the score
Challenge 11
Add
score = 0 as the first line of the program
Within the if commands (you have to do this for all 3
questions
if answer == correct:
print(“Right Answer")
score=score+1
else:
print(“Wrong Answer!”)
print(“The score is “,score,” out of 3”)
Challenge 11
correct=0
score = 0
print("Here is a quiz to test your knowledge of television")
print()
print("Question 1")
print("What type of animal is Shaun")
print()
print("a. Cow")
print("b. Dog")
print("c. Sheep")
correct = “c”
answer = input("Make your choice: ")
if answer == correct:
print(“Right Answer!")
score = score+1
else:
print("Wrong Answer!")
print("The score is ",score,"out of 3")
Challenge 12
We are now going to make another type of quiz
One which tests multiplication tables
Run the code on the next slide and check that
the program works
Can you explain what each line does?
Challenge 12
#import random library
import random
number1=random.randint(1,12)
number2=random.randint(1,12)
print("What is ", number1, " x ",number2, "?");
answer=input()
#change the answer into a number
answer=int(answer)
if answer == (number1*number2):
print("Correct")
else:
print("Wrong")
Challenge 13
This only does one question
We would like more than that so we will use a loop
The loop surrounds the program like this:
while question<6:
program
question=question+1
Everything that you want to happen in the loop must be
indented (moved in) 3 spaces
Challenge 13
#import random library
import random
question= 1
while question<6:
number1=random.randint(1,12)
number2=random.randint(1,12)
print("What is ", number1, " x ",number2, "?");
answer=input()
#change the answer into a number
answer=int(answer)
if answer == (number1*number2):
print("Correct")
else:
print("Wrong")
question=question+1
Challenge 14
Can you change the program to ask 10
questions?
Challenge 15
Can you change the program so that is keeps a
score?
Download