an introduction to python a series of introductory lessons to python that does not use too much...

34
An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Upload: philippa-boone

Post on 11-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

An introduction to Python

A series of introductory lessons to Python that does not use too much

maths!

Page 2: 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

Page 3: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 4: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 5: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 6: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 7: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 8: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Make the computer input

We are now going to change the program so that the computer accepts an input from the keyboard

Page 9: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 10: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Have four different vegetable

Instead of having just one input can you change the program so that it has four – one for each line.

Page 11: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 12: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 13: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 14: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

First part of the program

# To use random numbersimport randomveglist=[“Beans", “Brocolli", “Cabbage", “Cauliflower"]veg=random.choice(veglist)

Page 15: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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.")

Page 16: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 17: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 18: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 19: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 10

Can you add the other lines of the poem?

Page 20: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 11

Like to guess what these lines do?

msg ="Choose a vegetable?"choices = ["Pea", "Parsnip", "Aubergine", "Tomato"]veg = buttonbox(msg, title, choices)

Page 21: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 11

Add the lines to your program and see if they work

Challenge 12What does changing buttonbox for choice box

have?

Page 22: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 13

Let’s create a quiz program

Page 23: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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! “)

Page 24: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 25: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 14

Can you add two more questions?

Page 26: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 27: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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")

Page 28: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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")

Page 29: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

Challenge 16

Can you use the easygui commands (challenge 9-12) to make the quiz easier to use?

Page 30: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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?

Page 31: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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")

Page 32: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 33: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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

Page 34: An introduction to Python A series of introductory lessons to Python that does not use too much maths!

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)?