Hello : Projects
Hello : Projects
Hello 🌍🌎🌏
Write an interactive Python project that uses emoji
Find out what the Python programming language can do by writing an interactive project that uses emoji.
Emoji are small colourful images used to add extra meaning to messages. Emoji means ‘picture word’ in
Japanese.
You will:
print() text, including emoji, and get input() from the user
Store text and numbers in variables
Use functions to organise your code
Step 2 Say hello
It’s traditional to write a program to output ‘Hello world!’ when you learn a new
programming language.
Open the Hello 🌍🌎🌏 starter project (https://trinket.io/python/975f35023b). Trinket will open in
another browser tab.
The line #!/bin/python3 tells Trinket that you are using Python 3 (the latest version). The import lines tell Python
that you are going to use code you didn’t write.
Click below that line. The flashing | is the cursor and shows where you will type.
Type the code to print() hello:
main.py
11 #Put code to run under here
12 print('Hello')
Debug: If you get an error then check your code really carefully. In this example, the single quotes around
Hello are missing so Python doesn’t know it is supposed to be text.
In Python, a variable is used to store text or numbers. Variables make it easier for humans to read code. You can use
the same variable in lots of places in your code.
We have included some variables that store emoji characters.
In your Trinket, click on the emoji.py tab. Find the variable world, which stores the text ‘🌍🌍🌍’.
You can print() more than one item at a time by including a comma , in between the items. print() will
add a space between each item.
Click on the main.py tab to go back to your print() code.
Change your code to also print() the contents of the world variable:
main.py
11 #Put code to run under here
12 print('Hello', world)
Tip: 'Hello' is a text string because it has single quotes around it, whereas world is a variable so the
value stored in it will be printed.
Test: Run your code to see the result:
Emoji can look different on different computers, so yours might not look exactly the same.
Debug: Make sure that you have added a comma between the items in print() and that you have spelled
world correctly.
This example is missing the comma ,. It’s small but very important!
main.py
3 from noemoji import *
Add another line to your code to print() more text and emoji:
main.py
12 print('Hello', world)
13 print('Welcome to', python)
Tip: The code you need to type is highlighted in a lighter colour. Code that is not highlighted helps you find
where you need to add the new code.
Test: Click run.
Tip: It’s a good idea to run your code after every change so you can fix problems quickly.
Debug: Check carefully for brackets, quotes, commas, and correct spelling. Python needs you to be really
accurate.
If you have a Trinket account, you can click on the Remix button to save a copy to your My Trinkets library.
If you don’t have a Trinket account, you can still come back to your project in the future on the same computer by
using the starter project link.
+ add
- subtract
* multiply
/ divide
** to the power
Add another two print() lines to your code including a sum for Python to work out:
main.py
12 print('Hello', world)
13 print('Welcome to', python)
14 print(python, 'is very good at', sums)
15 print(230 * 5782 ** 2 / 23781)
Test: Run your code. Did Python calculate the sum correctly? Only joking! Python does the hard maths for
you so you don’t need to work it out.
Japanese computer scientist Emma Haruka Iwao used a computer to calculate the value of Pi (π) to 31 trillion
digits. That answer is so long that it would take over 300,000 years just to say it!
Test: Run your code and get Python to calculate your sum.
Debug: Make sure your sum has a left and right round bracket around it ( 2 * 45 ). If you use extra
brackets to control the order, make you have a right bracket to match every left bracket.
If you have asked Python to calculate a really big sum, you might find the answer goes across multiple lines
in the output area.
Tip: Click on the hamburger menu (the icon with three lines) in the top-left of your Trinket editor. Then click
on the Fullscreen button to view your project in fullscreen mode.
To exit fullscreen mode, click on the Fullscreen button again or press Esc on your keyboard.
The line from datetime import * at the top of the main.py tab includes a library with helpful functions for getting
the current date and time.
One of the great things about Python is all the libraries of code that are available to use. A Python library allows
you to easily use code that other people have written. There are libraries for drawing charts and graphs, making
art, doing calculations, and lots more.
Add another line to your code to print some more text and the emoji variables calendar and clock.
Get the current date and time by using the now() function from the datetime library:
main.py
14 print(python, 'is very good at', sums)
15 print(230 * 5782 ** 2 / 23781) #Print the result of the sum
16 print('The', calendar, clock, 'is', datetime.now()) #Print with emoji
Tip: You don’t need to type the comments, they are just there to help you understand the code. Just type
the part before the #.
Test: Run your code a couple of times to see the date and time update.
Debug: Check that you have a fullstop . between datetime and now. Check all the punctuation carefully.
In Python you call a function() to perfom an action. You have already used the print() function to output text.
You can define a new function to group code together so that you can name it and reuse it.
Functions need to be defined before you can call them. Look for the comment near the top of the main.py
tab that says #Put function definitions under here.
Define a new function called roll_dice() that uses the randint() function, from the random library, to
generate a random ‘integer’ (whole number) from 1 to 6 and output it to the screen.
main.py
7 #Put function definitions under here
8
9 def roll_dice(): #Don't forget the colon at the end of this line
10 print(python, 'can make a', dice)
11 print('You rolled a', randint(1, 6))
The lines under def roll_dice(): are indented. To do this, use the Tab character on your keyboard
(usually above CAPSLOCK on the keyboard). Indenting code tells Python that the indented lines are part of
the function.
Tip: The underscore _ is used to between words in variable and function names in Python to make them
easier to read. You can’t use a space.
Test: If you ‘Run’ your code now, it won’t roll a dice. That’s because you have defined the roll_dice()
function, but not called it yet.
To use a function, you need to call it in the code. Go to the end of your code and add a new line to call the
roll_dice() function:
main.py
20 print('The', calendar, clock, 'is', datetime.now())
21
22 roll_dice() #Call the roll dice function
Test: Run your project several times to see the random dice roll each time.
Debug: Make sure you have an underscore _ between roll and dice to make the function name. Make sure
you have a colon : at the end of the line.
Debug: Check that the lines under def roll_dice() are indented. It’s really common to get this wrong in
Python, so make sure to check.
Uses of random numbers include cryptography, data science, and adding variety into games and computer art.
Computers generate random numbers using an algorithm. For numbers that are really random, you need an
unpredictable input from the real world.
The fire variable stores a 🔥 emoji. The code print(fire * 3) outputs three fire emoji ‘🔥🔥🔥’. You need
to output the correct number of emoji to match the number rolled.
You would get a new random number that is usually different from your first random number.
Hmm, how can you make sure you use the same random number?
Change your code to save the value returned by randint() in a variable called roll and then use that
variable to print out the number rolled with the matching number of 🔥 emoji.
main.py
7 #Put function definitions under here
8
9 def roll_dice():
10 print(python, 'can make a', dice)
11 roll = randint(1, 6) #Generate a random number between 1 and 6
12 print('You rolled a', roll) #Print the value of the roll variable
13 print(fire * roll) #Repeat the fire emoji to match the dice roll
Test: Test your project a few times. Make sure you understand how the code works.
Tip: Variables are useful when you need to use the same value multiple times in your code. Giving variables
a sensible name also makes your code easier to understand.
Upgrade your dice so that the user can choose the maximum number.
Lots of games use many-sided dice. In the physical world, dice are made from regular geometric shapes.
Common dice include D6, D12, and D20. On a computer, you can generate a random number to make a fair dice
with any number of sides.
The input() function asks the user a question and then returns their answer.
Add code to ask the user for the biggest number on their dice and then save the result in a variable called
max and print the number chosen into the output area:
main.py
7 #Put function definitions under here
8
9 def roll_dice():
10 print(python, 'can make a', dice)
11 max = input('How many sides?:') #Wait for input from the user
12 print('That\'s a D', max) #Use the number the user entered
13 roll = randint(1, 6)
14 print('You rolled a', roll)
15 print(fire * roll)
To print an apostrophe ' in a word like That's, put a backslash \ before it so Python knows it’s part of the
text.
Change your roll variable code to use max as the maximum value for randint when it generates a
random number.
When you get input from the user, Python treats it as text. But, randint needs an ‘integer’ (a positive whole
number). The int function turns the user input into an integer.
main.py
7 #Put function definitions under here
8
9 def roll_dice():
10 print(python, 'can make a', dice)
11 max = input('How many sides?:') #Wait for input from the user
12 print('That\'s a D', max) #Use the number the user entered
13 roll = randint(1, int(max)) #randint needs max to be an 'integer'
14 print('You rolled a', roll)
15 print(fire * roll)
Test: Run your project. When the program reaches the input line, it will wait for you to enter a response
before continuing. Try it again with a different input number.
Add more print lines to your code. Here are some sentence starters that you can use:
main.py
print('I', heart, '...')
print('... makes me', happy)
print('I\'d like to make ... with', python)
Tip: If you need to print an apostrophe ' then you need to put a backslash \ before it so Python knows it’s
part of the text.
The yellow heart emoji is often used to show friendship and happiness.
What message would you send to a friend to cheer them up? Which emoji would you choose?
Click on the emoji.py tab in Trinket to see the emoji variables that you can use. Click back on the main.py
tab to add to your code.
You can add more emoji variables to emoji.py. Use this emoji list (https://unicode.org/emoji/charts/full-
emoji-list.html) to find the ones you want.
You could:
Use print with different text and emoji
Use input to get values from the user and store them as variables, then do sums with the variables
Write more functions to organise your code
Use # to add comments to your code
Click on emoji.py to see the emoji variables that you can use. Click back on main.py to add to your code.
Here’s an example function that asks about hobbies:
main.py
#Put code to run under here
def hobbies():
hobby = input('What do you like?')
print('That sounds', fun)
print('You could make a', python, 'project about', hobby)
Tip: Don’t forget you will need to call your function as well as define it.
You can use input to make your project wait for the user to tap Enter at any point in this project.
main.py
roll_dice()
hobbies()
Completed project
You can view the completed project here (https://trinket.io/embed/python/a54e164ac2).
If you want to have more fun exploring Python, then you could try out any of these projects (https://projects.raspbe
rrypi.org/en/projects?software%5B%5D=python).