Download presentation
Presentation is loading. Please wait.
1
An introduction to Python
A series of introductory lessons to Python that does not use too much maths!
2
Let us write a four line poem
Write a poem Let us write a four line poem
3
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.")
4
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
5
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.")
6
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.")
7
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.")
8
Make the computer input
We are now going to change the program so that the computer accepts an input from the keyboard
9
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.")
10
Have four different vegetable
Instead of having just one input can you change the program so that it has four – one for each line.
11
Challenge 5 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.") # for example veg1=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.")
12
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.")
13
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
14
First part of the program
# To use random numbers import random veglist=[“Beans", “Brocolli", “Cabbage", “Cauliflower"] veg=random.choice(veglist)
15
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.")
16
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
17
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
18
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") Enter the program as shown on the right. What do you think will happen? Run it and see
19
Challenge 10 Can you add the other lines of the poem?
20
Challenge 11 Like to guess what these lines do?
msg ="Choose a vegetable?" choices = ["Pea", "Parsnip", "Aubergine", "Tomato"] veg = buttonbox(msg, title, choices)
21
Challenge 11 Add the lines to your program and see if they work Challenge 12 What does changing buttonbox for choice box have?
22
Let’s create a quiz program
Challenge 13 Let’s create a quiz program
23
Challenge 13 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("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! “)
24
Challenge 13 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("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
25
Can you add two more questions?
Challenge 14 Can you add two more questions?
26
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
27
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+1 else: print("Wrong Answer! ") print("The score is "+str(score)+" out of 3")
28
Challenge 15 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("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 "+str(score)+" out of 3")
29
Challenge 16 Can you use the easygui commands (challenge 9-12) to make the quiz easier to use?
30
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?
31
Challenge 17 #import random library import 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 number answer=int(answer) if answer == (number1*number2): print("Correct") else: print("Wrong")
32
Challenge 18 This only does one question We would like more than that so we will use a loop The loop surrounds the program like this: question = 1 while question<6: program question=question+1 Everything that you want to happen in the loop must be indented (moved in) 3 spaces
33
Challenge 18 #import random library import random question= 1 while 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
34
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)?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.