programming in python lets learn some code together!

Post on 17-Jan-2018

250 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

PROGRAMMING IN PYTHONLETS 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.

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

-

/

*

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

LETS GIVE IT A TRY

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

LETS GIVE IT A TRY

LETS MAKE OUR FIRST SIMPLE PROGRAM

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

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

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.

# 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.

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.

LETS GIVE IT A TRY

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

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

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

LETS GIVE IT A TRY

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. ‘ ‘ ( ).

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)

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.

LETS MAKE A HARDER GAME…

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.

SHALL WE SHOW YOU WHAT YOU WILL BE CREATING?

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.

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

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

VARIABLE •Set a variable guessesTaken to 0.

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

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.

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)

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

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.

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

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

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.

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)

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

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

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.

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?

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.

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

SO ONCE THE GUESS LIMIT IS REACHED, WHAT HAPPENS?

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

if guess == number:

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)

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

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:

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)

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

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!

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!

CONGRATULATIONS YOU HAVE MADE YOUR GUESS THE NUMBER GAME!

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.

top related