Python Lesson 3 1. Starter Create a program which will ask people to type in the alphabet. It should...

Post on 19-Jan-2016

214 views 0 download

Tags:

transcript

Python

Lesson 3

1

StarterCreate a program which will ask people to type in the alphabet. It should show a message to see if they typed it correctly and a different message of

they didn’t type it in correctly.

2

Possible Solution:

3

Objective of the lessonWrite simple programs using Python.

• All of you will:– Use the random function in your programs.

• Most of you will:– Use For and While loops in your programs.

• Some of you will:– Create simple games using Python.

4

• Everybody should complete Task 1 to plan the steps to play a game called “Guess the fruit”.

• Some of you may also want to get the extra marks by completing the extension activity.

• This homework is due in next lesson.

• Make sure you have written your homework clearly in your planner.

Homework

5

What will this code do?import random is used tell Python that you want to be

able to use random numbers in your program.

random.randint(1,3) will create a random integer between 1

and 3.

6

• Tell user to enter either rock, paper or scissors.• Save their response in a variable.• Generate a random number from 1 to 3

(1=rock,2=paper, 3=scissors)• Compare the user’s selection and

the computer’s selection• Display who wins.

Rock … Paper … Scissors game

7

For loopsA For loop looks as follows:

number is a variable used to count.

range is a function which sets the upper and lower limits; for

instance range (10) would count from 0 to 9.

8

Try this outCreate a program that will ask the user to enter a number and then use the “For” loop to show the times table for that number; for instance if they entered a 3 then it will display:1 times 3 is 32 times 3 is 6 3 times 3 is 9…etc

Possible Solution:

9

While loopsA While loop looks as follows:

while starts a loop which will

continue until the condition has

been met.

10

Try this outCreate a program that will ask how many numbers you want

to add together. It will then ask you to enter the numbers (adding them to the previous total each time) and then once the loop has completed, display the total of all the numbers.

Possible Solution: