Python Basics Slides ÔÇô Ch3.1

Download Report

Transcript Python Basics Slides ÔÇô Ch3.1

[ slide 1 ]
while loop challenge!
This is a starter activity and should take 5 minutes
1. Log in to your computer
2. Open IDLE
3. The code below counts to 10
>>> number=1
>>> while number < 11:
print(number)
number = number+1
1. In interactive mode, re-write the code so it prints the
first 10 square numbers e.g. 1, 4, 9, 16, 25, 36 ...
[ slide 2 ]
Lesson Aims
In this lesson you are going to:
1. learn about saving code in Python’s script mode
2. learn how to write clear readable code
3. learn how to get keyboard input from users
4. write the start of a short game called MyMagic8Ball
Vocabulary
interactive mode
commenting
script mode
function
input
Script Mode
To
a new
script
mode
window:
Up open
to now
we have
not
saved
any files.
Open
IDLE
and then
from the File
menu choose New Window
We have
worked
in interactive
mode.
To
thissave
file:and run files too – in script mode:
Wesave
can also
Choose Save from the File menu.
[ slide 3 ]
[ slide 4 ]
Coding Time
1. Make a new folder in your Documents folder called: Python Code
2. Open IDLE and then a New Window from the File menu
3. Save this file as myMagic8Ball.py
(you must add .py to your file names)
4. Copy the code from Code Box 3.1 on page 30 in Python Basics into this
file and then save it again.
Top Tip: Check that the colour of the text in the code
matches that in the book exactly.
If it doesn’t, look to see whether you have forgotten to
open or close your speech marks.
If you are waiting:
1. Read the Delving Deeper section on page 31
2. Read the section about Writing Tidy Code on page 31 and 32
3. Read the Delving Deeper section on page 33
[ slide 5 ]
Code Analysis
# MyMagic8Ball
import random
# write answers
ans1="Go for it!"
ans2="No way, Jose!"
ans3="I'm not sure. Ask me again."
ans4="Fear of the unknown is what imprisons us."
ans5="It would be madness to do that!"
ans6="Only you can save mankind!"
ans7="Makes no difference to me, do or don't - whatever."
ans8="Yes, I think on balance that is the right choice."
[ slide 6 ]
# Commenting
Look
at this code.
It is possible
to add too much commenting.
Which
are variable
useful and
which
are unnecessary?
Often, comments
simply naming
and
function
names well is the best option.
The names
the programmer
are: (or elevator in the USA)
# This
is achosen
classbythat
describes here
a lift
Lift,Lift():
current_floor, getFloor, moveToFloor and floor_number
class
# The lift starts on floor zero
current_floor=0
# The method for finding the lift
def getFloor():
return current_floor
# The method for moving the lift
def moveToFloor(floor_number):
current_floor = floor_number
[ slide 7 ]
Naming variables
Variable names allow the coder and the computer to keep track of where a
space in the computer’s memory is.
Naming Guidelines:
• Always start a variable name with a lowercase letter.
• Use underscores to separate words.
• Use names that make sense.
e.g. my_number
[ slide 8 ]
Getting user input
To get a user’s keyboard input we use a Python built in function:
input()
How it works:
EXPERIMENT:
keyboard_input = input("Enter some text please")
Try out the experiment on page 35 in Python Basics to see this in action.
When the user presses the RETURN key after entering some text,
the string is stored in the variable keyboard_input
[ slide 9 ]
Homework
Write a Python file called friendly_computer.py
When run it should:
• ask the user for their name and store it in a variable
• say Hello to the user using the user’s name
• tell the user that it was nice talking to them
• say “Bye [name]”
Hint: You have done most of this in interactive mode (page 35)
Do not forget to save your file and send it to your teacher.
[ slide 10 ]
Lesson Summary
In this lesson you have learnt about:
1. saving code in Python’s script mode
2. writing clear readable code
3. getting keyboard input from users
Vocabulary
You should now know:
• the difference between and how to use: interactive and script
• when to use # commenting in your code
• how to use the input() function
modes