2. Silvia Tinena Coris
Table of contents
Part 1:
1. Learning Outcomes
2. Introduction
3. The first program: hello world!
4. The second program: hello (your name)!
5. More data types
6. Important things to know
7. If Statement
8. The third program: boy or girl?
9. For Statement
10. The fourth and fifth programs: your name + hello
students!
2
3. Silvia Tinena Coris
Table of contents
Part 2:
11. While Statement
12. The sixth program: security word
13. Functions
14. Seventh program: my functions
15. Interesting links
16. Exercises
3
6. Silvia Tinena Coris
While
6
The loop is executed as long as the condition
is true
Operands for the condition:
< less than
> greater than
== equal to
<= less than or equal to
>= greater than or equal to
!= different to
while (True):
…
Infinite loop!
10. Silvia Tinena Coris
Security word – level 2
10
If the user has typed the security word wrong 5
times, then wait for 30 seconds before allowing
the user to type it again.
To do so:
import time (at the very beginning of the
program)
when you want the delay: time.sleep(s)
being ‘s’ the number of seconds you want the program to
stop
11. Silvia Tinena Coris
Security word – level 3
11
If the user writes down the security word
wrong 5 times, wait for 30 seconds.
The second time that the user writes 5 wrong
security words, wait for 60 seconds.
The third time, wait for 90 seconds.
The fourth, 120 seconds.
And so on…
13. Silvia Tinena Coris
Security word – PRO
13
Now, we are going to emulate a login system
from any website/computer
First, you need to ask the user for a login
name
15. Silvia Tinena Coris
Functions
15
Useful for shortening the code and automatising
sets of actions that will be repeatedly used
together and in the same order
def function_name(internal_name_input1,
internal_name_input2):
…
print(output1, output2)
To call it:
function_name(value_1st_input,
value_2nd_input)
18. Silvia Tinena Coris
My functions
18
Create a function sum() and a function
multiply() that sums and multiplies
(respectively) all the numbers in a list of
numbers.
Make sure that it works with any list of
numbers, regardless of its length.
For that, create a couple of lists which have
different lengths and try to call the function with
each.
19. Silvia Tinena Coris
My functions – solutions
19
Now, try it yourself!
Create your own list and
use it in a function
20. Silvia Tinena Coris
My functions – solutions
20
Now, try it yourself!
Create your own list and
use it in a function
23. Remember to do the flowchart and the
pseudo code BEFORE programming!!!
16. Exercises23
24. Silvia Tinena Coris
0: A, B, C, D or E?
24
Take the pseudo code of the exercise where
the user is asked the result of an exam (0-100)
and the program returns the equivalent mark in
terms of A, B, C, D or E.
26. Silvia Tinena Coris
1: Histogram
26
Create a program which, given 7 inputs, writes
down a histogram. (Note that some of the
inputs can be 0)
eg. 2, 5, 6, 8, 10, 7, 11
**
*****
******
********
**********
*******
***********
30. Silvia Tinena Coris
2: 99 bottles of beer
30
Level 1:
Traditional song in the USA and Canada
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the
wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down, pass it around, 97 bottles of beer on the
wall.
etc.
Create a Python program which generates all the
verses of this song.
31. Silvia Tinena Coris
2: 99 bottles of beer
31
Level 2:
Modify the last verse like this (underlined):
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles
of beer on the wall.
No more bottles of beer on the wall, no more bottles
of beer.
Go to the store and buy some more, 99 bottles of
beer on the wall.
32. Silvia Tinena Coris
2: 99 bottles of beer
32
Level 3:
If you have developed your code using range,
now use while
If you have developed your code using while,
now use range
Level 4:
Learn the song and sing it!
36. Silvia Tinena Coris
3: age guess
Level 1: start with the basic functionality
Ask the year when the user was born
Display the age
Level 2:
Polish the program asking the user if he or she has
already celebrated the birthday this year. This will
mean a ± 1 year.
Level 3:
Greet the user and make a sentence with his or her
name plus his or her age
eg. (name), you’re (age) years old!
36
37. Silvia Tinena Coris
3: age guess
Level 4:
If the user has born this year, display a special
message
eg. You are still a baby!
Level 5:
Avoid negative ages by displaying a message if the
user enters a year superior to that we are in (2014)
eg. Oops! You aren’t born yet!
Level 6:
In the same situation as before, ask the user to come
back in X years’ time, when he or she will be born
eg. Come back in (X) years’ time!
37
38. Silvia Tinena Coris
3: age guess
Level 7:
Instead of pressing 1 or 2 to know if the birthday
has been celebrated yet, ask the user to write yes
or no (lowercase)
Level 8:
Create age blocks with personalised messages
for each
1~12: You are a child!
13~19: You are a teenager!
20~30: You are a youngster!
31~65: You are an adult!
>65: You are a senior!
38
41. Silvia Tinena Coris
The random function with a list
41
1. Import the random module at the very first line
of the program.
import random
2. Create a list of things to be randomly picked.
This means that if you want a random animal,
you will have to create a list of animals.
3. Assign to a variable a random value of the list.
my_fav_animal = random.choice(animals)
4. Now you can use my_fav_animal as usual!
42. Silvia Tinena Coris
The random function with a list
42
Now, try it yourself!
Create your own list
and randomly pick
one of its items
43. Silvia Tinena Coris
The random function with numbers
43
1. Import the random module at the very first line
of the program.
import random
2. Assign to a variable a random value of the
defined range.
random_num = random.randint(1,100)
3. Now you can use your random_num as
usual!
44. Silvia Tinena Coris
The random function with numbers
44
Now, try it yourself!
Randomly pick one
number from a
defined range
45. Silvia Tinena Coris
4: Story time
45
You are going to create a story which will be
different every time you run the program
For that, we will create different lists and use
the random function
46. Silvia Tinena Coris
4: Story time
Ask the user his or her name and gender and
store them in appropriate variables
Create a variable called ‘pronoun‘ which will
obviously be he, she or it. Assign it depending
on the gender
Create four lists:
Roles, names, actions and places
eg. roles = [‘knight’, ‘princess’,
‘prince’, ‘wizard’]
They can have as many items as you want and
not all the list have to have the same number of
words
46
47. Silvia Tinena Coris
4: Story time
Now write the story, all in the same line!
Suggestion: write the story in a variable and then
do a print of this variable
If you aren’t feeling imaginative, you can
borrow this story:
Once upon a time, there was a (role) called (user’s name).
(pronoun) and some friends found themselves in the magic
land of (place). This land was ruled by (name) the (role2).
All of a sudden a mysterious voice spoke to them from high in
the sky and said: “you must (action) (name) the (role2) to lift
the curse of not being able to use technology…”
47
49. Silvia Tinena Coris
5: Guess the number
49
Level 1:
The program will randomly generate a number
between 1 and 100 and the user will have to
guess it.
To help the user, the program will “say” whether
the number to be guessed is higher or lower than
that the user entered.
Level 2:
Interact with the user and display personalised
messages with his or her name.
50. Silvia Tinena Coris
5: Guess the number
50
Level 3:
Count how long does it take to the user to guess
the number and display different messages
depending if the user has guessed it in the first
round or not.
Level 4:
If the number guessed by the user is really close
to the actual number (let’s say it’s 5 or less units
lower or higher), display a special message
51. Silvia Tinena Coris
5: Guess the number – a bit of help
51
Level 4:
if users_num < random_num:
if users_num – random_num <= 5
print (‘You’re almost there! Just a little bit higher!’)
if users_num > random_num:
if users_num – random_num >= 5
print (‘You’re almost there! Just a little bit lower!’)
52. Silvia Tinena Coris
6: Hangman
52
I guess you all know how to play hangman!
The user can only fail 5 times
Level 1:
Fixed word
The remaining guesses are just a number on the
screen (not the actual hangman)
Level 2:
List of words, random choice of them
53. Silvia Tinena Coris
6: Hangman
53
Level 3:
Display a hangman. You can do one like this:
O
_|_/
|
/
d b
Level 4:
Give the option that another user writes down the secret
word! (If there is only one player, the program will choose
the secret word. If there are two players, the first user will
write the word down and the second will have to guess
it).
Hint: after the 1st player has written down the word, introduce
a lot of spaces so the 2nd player can’t see the word!
56. Any comments or questions? Drop me an email:
Silvia.tinena.coris@gmail.com
Or add me on LinkedIN:
Silvia Tinena Coris
Thanks!
Hope you’ve learned 56