programming in python lets learn some code together!

54
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

Upload: esther-hines

Post on 17-Jan-2018

254 views

Category:

Documents


0 download

DESCRIPTION

PYTHON CAN DO SIMPLE MATHS Python uses these symbols to do simple Maths, what do you think each one does? +-/*+-/*

TRANSCRIPT

Page 1: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

PROGRAMMING IN PYTHONLETS LEARN SOME CODE TOGETHER!

Page 2: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

CHALLENGE •If you know how to make games in Python, your task it to surprise us with what you can come up with.•If not we are going to create a few games together in Python now.

Page 3: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

PYTHON CAN DO SIMPLE MATHSPython uses these symbols to do simple Maths, what do you think each one does?+

-

/

*

Page 4: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

VARIABLES•What’s a variable?•Something that is stored for later use and can be called if needed.•Variables can be any data type.

Page 5: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS GIVE IT A TRY

Page 6: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

STRINGS•Variables can contain text too, these are called strings.•We use quote marks to set what text the variable will hold. •Variable = ‘ text ‘

Page 7: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS GIVE IT A TRY

Page 8: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS MAKE OUR FIRST SIMPLE PROGRAM

Page 9: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WHAT WILL IT DO?•Together we will make a program to say hello and ask for a name.•It will then use the name that is inputted to say, it is good to meet you (name).

Page 10: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

Page 11: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

STEP 1 •Click on File, New Window.•This is the File Editor.•Click Save and save it as Hello World in your user area. •Make a folder called Python to put it in if you want.

Page 12: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

# TAGS•A # tag can be used to write a comment to anyone who reads your code. •Try this now, tell the reader what the program will be doing.

Page 13: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

PRINT COMMAND•What do you think a print command might do?•It prints whatever you ask it to onto the screen of the user.

•Just like strings the text needs to have quote marks around it but also needs to have brackets ( ) outside of them too.

Page 14: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS GIVE IT A TRY

Page 15: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…# This program says hello and asks my name.print (‘Hello World’)print (‘What is your name?’)

Page 16: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

ADDING A VARIABLE•Now we need a variable to hold a name value.

Page 17: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

INPUT COMMAND•The input command will take what the user inputs into the program.•It can be used to put inputted text into a variable.•It looks like this:

Name = input ()

Page 18: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS GIVE IT A TRY

Page 19: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

NOW WE CAN USE THE NAME TO SAY HELLO…•Try to work out how you might do this.•You need the print command, the + symbol, the variable and the other symbols needed in each command e.g. ‘ ‘ ( ).

Page 20: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…# This program says hello and asks my name.print (‘Hello World’)print (‘What is your name?’)name = input()print (‘It is good to meet you, ‘ + name)

Page 21: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LET’S RUN THE PROGRAM!•Save the program again.•Click Run, Run Module.•The program should start.•If it errors there is a mistake in your code, try to fix it yourself, if you cant ask a friend, as a last resort ask a teacher.

Page 22: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LETS MAKE A HARDER GAME…

Page 23: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WHAT WILL IT DO?•This game will ask the users name and ask them to play a guess the number game.•The user will have 6 guesses, but they will be told if they are guessing too high or low.•Once they guess correctly it will tell them how many tries it took to get it correct.

Page 24: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

Page 25: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

STEP 1 •Click on File, New Window.•This is the File Editor.•Click Save and save it as Guess the Number in your user area. •Make a folder called Python to put it in if you want.

Page 26: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

# TAG TIME•Write a # tag comment to anyone who reads your code.

Page 27: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

IMPORT•Type import random•This imports several functions related to random numbers.•We will come back to this later.

Page 28: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

VARIABLE •Set a variable guessesTaken to 0.

Page 29: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

ASK FOR THEIR NAME •Ask for their name and remember to tell the computer to add it to a variable called name using, input()

Page 30: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SETTING THE NUMBER TO BE FOUND•To do this we need a variable called number and the random statement again.•Type in the following:number = random.randint (1, 20)This asks for the computer to import a random integer (number) between 1 and 20.

Page 31: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…# This program is a guess the number

game.import randomguessesTaken = 0print (‘Hello! What is your name?’)name = input()number = random.randint (1, 20)

Page 32: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

USE THE NAME VARIABLE•Use the name variable to tell the player you are thinking of a number between 1 and 20.

Page 33: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WHILE LOOPS•What is a while loop?•A while loop will perform an action (set by you) while a condition is being met (also set by you).

•We need a while loop that asks the player to take a guess while the number of guesses taken is less than 6.

Page 34: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WHILE LOOPS while guessesTaken <6: print (‘Take a guess’) # indent this line by 4 spaces

Page 35: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

GUESS VARIABLE•Now we need a guess variable that will contain what the user inputs. •How might we do this?

Page 36: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SETTING THE INPUT TO AN INTEGER•The input at the moment will be classed as a string of text, even though we know it is a number the computer does not.

Page 37: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SETTING THE INPUT TO AN INTEGERWe need to tell the computer that the inputted data is an integer. This is how we do it:guess = int(guess)

Page 38: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

TELL THE COMPUTER THE USER HAS MADE A GUESS•Now the computer needs to increase the variable guessesTaken by 1.•How might we do this?guessesTaken = guessesTaken + 1

Page 39: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…

number = random.randint (1, 20)print (‘Well, ‘ + name + ‘, I am thinking of a number between 1 and 20.’)

Page 40: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…

while guessesTaken < 6: print (‘Take a guess’) guess = input() guess = int(guess) guessesTaken = guessesTaken + 1

To tell the computer the while loop is still running we must indent all of the content by 4 spaces.

Page 41: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

IF STATEMENTS •An if statement will tell the computer to do something if a condition has been met, if it has not it wont compete the command set by you.•There are other types of if statements. Can you think of any?

Page 42: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

IF STATEMENTS if guess < number: print (‘Your guess is too low’) if guess > number: print (‘Your guess is too high’) if guess == number: break

== means equal to

The if statements are indented by 4 spaces and the contents of the if statements by another 4 spaces.

Page 43: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

THE WHILE LOOP IS OVER!•All code should now start without an indent.

Page 44: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

SO ONCE THE GUESS LIMIT IS REACHED, WHAT HAPPENS?

Page 45: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

IF THEY GUESSED CORRECTLY•Think how we could use an if statement to do a command if they guessed correctly.

if guess == number:

Page 46: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WHAT WOULD THE COMPUTER DO?•First we need the convert the number of guesses back into a string so we can display it for the user. •How might we do this?guessesTaken = str(guessesTaken)

Page 47: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

THEN CONGRATULATE THEM AND TELL THEM THEIR GUESSESprint (‘Good job, ‘ + name + ‘! You guessed the number in ‘ + guessesTaken + ‘guesses!’)

Page 48: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

IF THEY GUESSED INCORRECTLY•Think how we could use an if statement to do a command if they guessed correctly.

!= means not equal to.

if guess != number:

Page 49: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

WE CONVERT THE NUMBER VARIABLE•This time we convert the random number chosen at the start into a string so we can display it.•How might we do this?number = str(number)

Page 50: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

THEN TELL THEM THEY WERE NOT SUCCESSFUL! print (‘Nope, the number I was thinking of was ‘ + number)

Page 51: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…

if guess == number: guessesTaken = str(guessesTaken) print (‘Good job, ‘ + name + ‘! You guessed the number in ‘ + guessesTaken + ‘guesses!’)

Notice the indents!

Page 52: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

BY NOW YOU SHOULD HAVE SOMETHING LIKE THIS…

if guess != number: number = str(number) print (‘Nope, the number I was thinking of was ‘ + number)

Notice the indents!

Page 53: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

CONGRATULATIONS YOU HAVE MADE YOUR GUESS THE NUMBER GAME!

Page 54: PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!

LET’S RUN THE PROGRAM!•Save the program again.•Click Run, Run Module.•The program should start.•If it errors there is a mistake in your code, try to fix it yourself, if you cant ask a friend, as a last resort ask a teacher.