File

COMPUTER PROGRAMMING
Remember from last week, there are 2 modes that you can
program with in PYTHON…
INTERACTIVE Mode
>>> (has chevrons)
SCRIPT Mode
Used for writing larger programs
and requires F5 to run.
Open PYTHON and go to SCRIPT Mode
(Go to FILE menu, then click New Window)
Write the following code into your PYTHON window
and save it as
InteractiveProgram.py
print("Please type your name in")
my_name = input ()
What is the purpose of my_name in
this program?
A variable is a place in the computer’s memory
that holds a temporary value.
We can place data into a variable by ASSIGNING it
to the variable.
Variable_Name
Assignment
In Python, it looks like this
Data
Name = ‘Tom’
print("Please type your name in")
my_name = input ()
input () is a useful function that prompts the
user to enter some data.
It means that whatever the user types in will be
ASSIGNED to the variable my_name
Save & Run the program (F5) to test it.
Edit your program and add the bottom line of code…
print("Please type your name in")
my_name = input ()
print(“Nice to meet you” ,my_name)
Python allows us to print a bunch of letters
(known as a string) followed by a variable!
BUT!
Make sure you separate the two things with a coma!
Re-test the program by running it.
Edit your program further by adding the last three lines of code.
Save it and then Run it (F5)
print("Please type your name in")
my_name = input ()
print("Nice to meet you ", my_name)
print("So ", my_name , " what is your favourite food?")
favourite_food = input ()
print("Ah, your favourite food is " , favourite_food)
How does this program
work?
Edit your program further by adding the last three lines of code.
Save it and then Run it (F5)
print("Please type your name in")
my_name = input ()
print("Nice to meet you ", my_name)
print("So ", my_name, " what is your favourite food?")
favourite_food = input ()
print("Ah, your favourite food is " , favourite_food)
QUESTIONS … you have 2 minutes! (Teacher > Click slide to start timer)
1. Write down the name of a variable used in the above program
2. How many variables are used in the above program
3. What kind of character/symbol is used to assign values to a variable
4. Write down an example of assignment
5. Katie is writing her first program. It won’t run properly. Can you spot the
Syntax error?
Print(‘Hello World’)
1:12
1:13
1:14
1:15
1:16
1:17
1:18
1:19
1:20
1:21
1:22
1:23
1:24
1:25
1:26
1:27
1:28
1:29
1:30
1:31
1:32
1:33
1:34
1:35
1:36
1:37
1:38
1:39
1:40
1:41
1:42
1:43
1:44
1:45
1:46
1:47
1:48
1:49
1:50
1:51
1:52
1:53
1:54
1:55
1:56
1:57
1:58
1:59
2:00
1:00
1:01
1:02
1:03
1:04
1:05
1:06
1:07
1:08
1:09
1:10
0:12
0:13
0:14
0:15
0:16
0:17
0:18
0:19
0:20
0:21
0:22
0:23
0:24
0:25
0:26
0:27
0:28
0:29
0:30
0:31
0:32
0:33
0:34
0:35
0:36
0:37
0:38
0:39
0:40
0:41
0:42
0:43
0:44
0:45
0:46
0:47
0:48
0:49
0:50
0:51
0:52
0:53
0:54
0:55
0:56
0:57
0:58
0:59
0:01
0:02
0:03
0:04
0:05
0:06
0:07
0:08
0:09
0:10
1:11
0:11
End
If you can, do all these programs in SCRIPT mode
(i.e. go to FILE, NEW WINDOW…then press F5 to run)
Task: Create a program in python that prompts the user for their name AND the names
of their top 5 favourite films. Each film should be assigned to a separate variable. When
run, the computer should output the following…
Hello [my_name], your top 5 favourite films are as follows:
[favourite_film1]
[favourite_film2]
[favourite_film3]
[favourite_film4]
[favourite_film5]
Here is an example
Save as
‘Favourite_Films.py’
Hello Tim, your top 5 favourite films are as follows:
The Thin Red Line
City Of Angels
The Breakfast Club
http://www.computingacademy.org.uk/programming/
Terminator 2 Judgement Day
introduction-programming-python l
The Return of the King