Upload
philippa-boone
View
244
Download
0
Embed Size (px)
Citation preview
An introduction to Python
A series of introductory lessons to Python that does not use too much
maths!
Write a poem
Let us 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 alter 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 exampleprint("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 5Change hereChange hereChange hereChange hereprint("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.")
# for exampleveg1=input ("What is vegetable 1? ")
veg2=input("What is vegetable 2? ")………….
# for example
print("Mashed "+veg1+" on the floor")
print("Green "+veg2+"on the floor.")
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 numbersimport randomveglist=[“Beans", “Brocolli", “Cabbage", “Cauliflower"]veg=random.choice(veglist)
Challenge 6#to use random numbersimport randomveglist=["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 7Create 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 8Create a program to roll a six sided dice
Challenge 9
Unfortunately our program does not look too good at the moment.
We can make it look a lot better by using a library of commands called easygui
The gui stands for Graphical User Interface
GUI Poem#load the easygui commands
from easygui import *
title="Poem“
veg=enterbox("Type the name of a vegetable ")
message=("Our poem is about a "+veg)
msgbox(message, title)
message=("Mashed "+veg+" on the ceiling")
msgbox(message, title)
Enter the program as shown on the right.
What do you think will happen?
Run it and see
Challenge 10
Can you add the other lines of the poem?
Challenge 11
Like to guess what these lines do?
msg ="Choose a vegetable?"choices = ["Pea", "Parsnip", "Aubergine", "Tomato"]veg = buttonbox(msg, title, choices)
Challenge 11
Add the lines to your program and see if they work
Challenge 12What does changing buttonbox for choice box
have?
Challenge 13
Let’s create a quiz program
Challenge 13correct = 0print("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 13correct = 0print("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! ")
Try to think of a reason for the two equal signs
It is called a logic test
Challenge 14
Can you add two more questions?
Challenge 15
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 15
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+1else: print("Wrong Answer! ")print("The score is "+str(score)+" out of 3")
Challenge 15correct=0score = 0print("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+1else: print("Wrong Answer!")print("The score is "+str(score)+" out of 3")
Challenge 16
Can you use the easygui commands (challenge 9-12) to make the quiz easier to use?
Challenge 17
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 17#import random libraryimport random
number1=random.randint(1,12)number2=random.randint(1,12)
print("What is "+str(number1)+ " x "+str(number2)+" ?");answer=input()#change the answer into a numberanswer=int(answer)
if answer == (number1*number2): print("Correct")else: print("Wrong")
Challenge 18This only does one question
We would like more than that so we will use a loop
The loop surrounds the program like this:
question = 1while question<6: program question=question+1
Everything that you want to happen in the loop must be indented (moved in) 3 spaces
Challenge 18#import random libraryimport randomquestion= 1while question<6: number1=random.randint(1,12) number2=random.randint(1,12)
print("What is "+str(number1)+" x "+str(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 19
Can you change the program to ask 10 questions?
Challenge 20
Can you change the program so that is keeps a score and is easier to use (use easygui)?