elimPythonIntro

advertisement
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 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
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“
Enter the
program as
shown on the
right.
What do you
think will
happen?
Run it and see
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)
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 12
What does changing buttonbox for choice box
have?
Challenge 13
Let’s create a quiz program
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()
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 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()
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 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+1
else:
print("Wrong Answer! ")
print("The score is "+str(score)+" out of 3")
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()
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")
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 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")
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
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
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)?
Download