More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
◑ Python IDE Dashboard

Computing Operators

While programming with high-level languages such as Python, you will use four different types of operators as follows:

  • The assignment operator ( = ) which is used to assign a value to a variable or a constant.
  • Arithmetic operators ( +, -, *, / ) which are used to perform mathematical calculations.
  • Comparison operators ( >, <, >=, <=, ==, != ) which are used to compare values.
  • Boolean operators ( AND, OR, NOT ) which are often used to complete more than one comparison.

Match the dominoes!


Click on the picture below to start:
operators-dominoes

Tagged with: , , , ,

Beach Huts Challenge

beach-hutsLook at the code below to draw lines of beach huts.

(X,Y) Coordinates


The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high.
Look at the canvas below to understand how (x,y) coordinates work:

Python Code

Your task is to tweak the code used to draw the third line of beach huts. On this line we want the computer to choose random colours from the colour list – that’s already the case! However this time we want to make sure that we cannot have two beach huts of the same colour next to each other.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: , ,

Operators Quiz

While programming with high-level languages such as Python, you will use four different types of operators as follows:

  • The assignment operator ( = ) which is used to assign a value to a variable or a constant.
  • Arithmetic operators ( +, -, *, / ) which are used to perform mathematical calculations.
  • Comparison operators ( >, <, >=, <=, ==, != ) which are used to compare values.
  • Boolean operators ( AND, OR, NOT ) which are often used to complete more than one comparison.

Take the Quiz! (open full screen)

Guess the country

globeFor this challenge we are going to create a game where the user plays against the computer.

The aim of this game is to guess the name of a country. The computer will give the player clues to help them identify the country. The aim for the player is to try to guess the country using as few clues as possible.

For this challenge we are going to work from a flowchart that describes the algorithm we will base our code on: (Click on the picture to zoom in)

GuessTheCountry

Let’s get coding


We have started the code for you. Use the flowchart above to complete this code.

Extension task #1


Could you tweak this code to display at the end how many clues have been used in total to guess the country.

Extension task #2


This code only works with one country.

How could we change it to have a list countries and a list of clues for each country and get the computer to randomly choose one country from the list of countries.

Tip: To solve this task you may want to investigate how to use a list of lists in Python.

Tagged with: , ,

Question Time

Feeling confident with your computing knowledge?

Spend a few minutes to answer or research the following questions:


?
Why are there around 16 milion colours in the RGB colour palette?


?
Why would a text file containing 3,000 characters use around 3 kilobytes?


?
Why are there 1024 (and not 1000) bits in a kilobyte?


?
Why do we use RGB colour codes when the three primary colours are Red, Yellow and Blue.


?
What is the expression:
= != ==
telling us?


?
Is the boolean expression:
(NOT A) AND (NOT B)
the equivalent of
NOT (A OR B)

?


?
How can we tell that #333333 is a darker grey colour than #AAAAAA?


?
How can we tell that #FF00FF is a magenta/purple colour?


?
Why do we store phone numbers or credit card numbers as strings and not integers?


?
Why do bitmap pictures pixelate when they are resized/enlarged but vector-based pictures don’t?


?
What is the maximum number of devices that we can connect on the Internet at the same time (using IPv4)?


?
Does it take longer to download a file from a sever located in Australia or in Europe?


?
How does internet data cross the Atlantic Ocean?


?
How does Google search the entire web in just a few milliseconds?


?
Would it be possible for a computer to have a sense of humour?

Secret Code

In this challenge we are putting your deciphering skills to the test.

Here are your secret codes. Will you be able to decode these?

Cipher #1

0101010001101000011001010010000001000001010100110100001101001001010010010010000001100011011011110110010001100101001000000110100101110011001000000111010101110011011001010110010000100000011101000110111100100000011000110110111101101110011101100110010101110010011101000010000001110100011001010111100001110100001000000111010001101111001000000110001001101001011011100110000101110010011110010010000001100011011011110110010001100101001000010010000001010100011010000110010100100000010000010101001101000011010010010100100100100000011000110110111101100100011001010010000001100011011011110110111001110100011000010110100101101110011100110010000000110001001100100011100000100000011000110110100001100001011100100110000101100011011101000110010101110010011100110010111000100000010101000110100001100101001000000110010101111000011101000110010101101110011001000110010101100100001000000100000101010011010000110100100101001001001000000110001101101111011001000110010100100000011000110110111101101110011101000110000101101001011011100111001100100000001100100011010100110110001000000110001101101000011000010111001001100001011000110111010001100101011100100111001100101110

Cipher #2

\u0055\u006e\u0069\u0063\u006f\u0064\u0065 \u0069\u0073 \u0061 \u0063\u0068\u0061\u0072\u0061\u0063\u0074\u0065\u0072 \u0073\u0065\u0074 \u0075\u0073\u0069\u006e\u0067 \u0032 \u0042\u0079\u0074\u0065\u0073 \u0070\u0065\u0072 \u0063\u0068\u0061\u0072\u0061\u0063\u0074\u0065\u0072 \u0077\u0068\u0069\u0063\u0068 \u006d\u0065\u0061\u006e\u0073 \u0069\u0074 \u0063\u0061\u006e \u0072\u0065\u0070\u0072\u0065\u0073\u0065\u006e\u0074 \u006d\u006f\u0072\u0065 \u0074\u0068\u0061\u006e \u0036\u0035\u002c\u0030\u0030\u0030 \u0075\u006e\u0069\u0071\u0075\u0065 \u0063\u0068\u0061\u0072\u0061\u0063\u0074\u0065\u0072\u0073\u002e \u0054\u0068\u0069\u0073 \u0069\u0073 \u0077\u0068\u0079 \u0075\u006e\u0069\u0063\u006f\u0064\u0065 \u0069\u0073 \u0075\u0073\u0065\u0064 \u0074\u006f \u0065\u006e\u0063\u006f\u0064\u0065 \u004a\u0061\u0070\u0061\u006e\u0065\u0073\u0065 \u0063\u0068\u0061\u0072\u0061\u0063\u0074\u0065\u0072\u0073 \u0028\u0065\u002e\u0067 \u6628\u002c\u591c\u002c \u6700\u002c\u9ad8\u0029 \u006f\u0072 \u0041\u0072\u0061\u0062\u0069\u0063 \u0063\u0068\u0061\u0072\u0061\u0063\u0074\u0065\u0072\u0073 \u0028\u0065\u002e\u0067\u002e \u0635 \u002c\u0639 \u0634 \u002c\u062c\u0029\u002e
Tagged with: , , ,

Data Types Quiz

When using variables and constants in your programs it is important to use the correct data types.

The main data types are:

  • Integer: for whole numbers only such as 7,
  • Float/Real: for numbers with decimals only such as 9.81,
  • String: for piece of text. It can also include number digits and punctuation signs. e.g. “IP4 5HD”, “Hello world!”,
  • Boolean: for values which are either True or False,
  • Date & Time: for dates! (With or without a time): e.g. 31/12/2015 00:00:00, 25/12/2015

Test your knowledge of Data Types (open full screen):


Tagged with:

Haunted House

haunted-houseYou are going to write a Python game where the user walks within the different rooms of a haunted house.

In each room, something will happen. You will decide what happens when the user enters a room. You can use some of the following ideas:

  • Maybe a ghost will appear (ASCII Art using the print command) when the user enters the bedroom,
  • You could have three magic potions on the kitchen table and your program could ask the user to choose one of them. It would then display a different message based on the potion chosen by the user,
  • There could be a secret passage in the library to lead to the kitchen,
  • There could be a random math question to solve in the living room, and if the user does not give the correct answer they lose.

Designing the game


Open and print the following floor plan. Write in each room what you would like your program to do when the user enters the room.

hauntedhouse

Let’s get coding


We have started the game for you with some (but not all) the options for the user to walk from one room to the other.

Complete this game, one room at a time:

Code for the library() function

Here is an example of code for the library() function. Looking at the floor plan of the mansion, you will notice that there is only one door to go back to the hall. However we are going to add a secret passage to lead to the kitchen.

def library():
  print("--- You are entering the library ---")
  print("")
  time.sleep(1)
  print("While looking at the books on the bookshelves, you notice that one of the book is not perfectly aligned with the other books.")
  print("""
                       ┌─┐
   ┌─┐  ┌─┐   ┌──┐     │ │
┌──┤ │  │ ├───┤  ├─────┤ │
│  │ ├──┤ │   ├──┤     │ ├───┐
│  ├─┤xx│ │   │┼┼│ ─── │ │   │
│  ├─┤  │ ├───┼──┤     │ │ ┌─┤
│  │ │  │ ├───┤  │ ─── │ │ └─┤
│  │ │  │ │   │  │     │ │   │
│  │ │  │ │   │  │     │ │   │
└──┴─┴──┴─┴───┤  ├─────┴─┴───┘
              └──┘
  """)
  time.sleep(1)
  option = input("Do you: \n a) pick up this book \n b) Leave this book where it is and go back to the hall").lower()
  if option == "a":
      print("When trying to pick up the book, the book acts as a lever and the bookshelf slides to the left revealing a secret passage.")
      time.sleep(1)
      print("Feeling adventurous, you decide to go through this secret passage and realise that it leads you to the kitchen.")
      time.sleep(1)
      kitchen()
  elif option == "b":
      print("Hum... I do wonder why this book was not perfectly aligned? Anyway, let's go back to the hall...")
      time.sleep(2)
      hall()

Note that for this code to work, you will also need to define a new function for the kitchen:

def kitchen():
  print("--- You are entering the kitchen ---")
  print("")
  time.sleep(1)
  #complete the code here

Using an inventory

We will now add an inventory to our code to let the player collect objects as they progress through the game. To do so we will first initialise a new empty list called inventory using the following line of code that we will place at the very beginning of our code (e.g. on line 3)

inventory = []

Then we will add some code to our kitchen() function for the player to pick up a golden key.
In Python, we can check if a value is in a list using the keyword in.
For instance we will only display the key on the floor of the kitchen, if the user has not already picked it up using the following line of code:

if "Golden Key" not in inventory:

To add a value to a list we will use the append() function as follows:

inventory.append("Golden Key")

Let’s use this code to let the user pick up the key from the kitchen floor.

def kitchen():
  print("--- You are entering the kitchen ---")
  print("")
  time.sleep(1)
  
  if "Golden Key" not in inventory:
    print("""
         ┌──────┐
         │      │
         │      ├────────┬─┬──┬─┐
         │      │        │ ├──┤ │
         └──────┘        └─┘  └─┘
    """)
    print("As you enter into the kitchen, you notice a golden key on the floor and decide to pick it up as it could become useful later on.")
    inventory.append("Golden Key")
    print("Inventory:")
    print(inventory)
    #Add code here to decide where to go next

Then, in other rooms, you will be able to check if the user has the key to for instance let them open a treasure chest.

def bedroom():
  print("--- You are entering bedroom ---")
  print("")
  time.sleep(1)
  print("Hidden under the bed, you find a treasure chest.")
  if "Golden Key" in inventory:
     print("You use your golden key to open it and found a silver crown inside it.")
     inventory.append("Silver Crown")
     print("Inventory:")
     print(inventory)
  else:
     print("The treasure chest is locked and you do not seem to have the key to unlock it!")
  #Add code here to decide where to go next
 
Tagged with: , , ,

Computing Terminology

Check your computing terminology by joining the dominoes below: (Click on picture to start).

dominoes

Computing TerminologyOpen Domino Activity
 

Yes or No!

yes-no

Learning Objectives


When your computer prorgam is asking questions to the end-user, the end-user may not answer these questions the way you expected them to. This may cause your program to not work properly. Ideally a good program should use some form of validation checks to ensure that the user is providing their answers in the correct format.

Let’s look at the following code that asks a simple Yes/No question:

answer = input("Are you over 16?")
if answer == "Yes":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "No":
   print("You are too young to learn how to drive a car.")

What would happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?

Now let’s look at how we could improve this code further:

Solution #1Explanations
answer = input("Are you over 16?").upper()
if answer == "YES":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
   print("You are too young to learn how to drive a car.")
Check the code, did you notice that we added .upper() at the end of line 1?
This will convert the user input into capital letters.
Then, within our if statements, we can compare the user input with “YES” and with “NO”.

What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #2Explanations
answer = input("Are you over 16?").upper()
if answer == "YES":
   print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
   print("You are too young to learn how to drive a car.")
else:
   print("Sorry your answer is not recognised. Restart the program and make sure you answer with the word Yes or the word No.")
By adding an else statement we can give some feedback to the end-user to tell them why their answer is not valid and how they need to answer the question next time

What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #3Explanations
answer = ""
while (answer!="YES" and answer!="NO"):
  answer = input("Are you over 16? (Yes or No)").upper()

if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
Using a while loop, we keep asking the question up until the user has typed a valid answer (Yes or No).
What do you think will now happen if:

  • The user answers Yes ?
  • The user answers No ?
  • The user answers YES ?
  • The user answers no ?
  • The user answers Not yet ?
Solution #4Explanations
def askYesNoQuestion(question):
  YesNoAnswer = ""
  while (YesNoAnswer!="YES" and YesNoAnswer!="NO"):
    YesNoAnswer = input(question).upper()
  return YesNoAnswer

answer = askYesNoQuestion("Are you over 16? (Yes or No)")
if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
By creating our own askYesNoQuestion() function we can easily reuse this function every time we want to ask a YesNo question! In our main program, it is as easy as using an input command. It is however a lot more effective because it makes sure the user gives a valid answer!
Solution #5Explanations
def askYesNoQuestion(question):
  YesNoAnswer = input(question).upper()
  if YesNoAnswer == "YES" or YesNoAnswer == "NO":
     return YesNoAnswer  
  else:
     return askYesNoQuestion(question)

answer = askYesNoQuestion("Are you over 16? (Yes or No)")
if answer == "YES":
  print("You can take driving lessons to prepare for your driving test.")
elif answer == "NO":
    print("You are too young to learn how to drive a car.")
This time our askYesNoQuestion() function is not using a while loop but it is using recursion: It’s calling itself. Check on line 6 for the recursive call to itself. And it will keed doing so until it receives a valid answer.

Extension Task:


These different approaches to validate a user input only work for “Yes” / “No” questions.
Would you be able to adapt these to make them work for:

  • Multiple answers questions? e.g. Which Science subject do you prefer: Biology, Physics, Chemistry or Computer Science?
  • Number based questions? e.g. Which year group are you in: Only accept a value between 1 and 13. (Tip you will need to use < and > comparison operators to validate the user input.)
Tagged with: , , ,
Privacy Overview

This website will store some information about your preferences on your own computer inside a tiny file called a cookie. A cookie is a small piece of data that a website asks your browser to store on your computer or mobile device. The cookie allows the website to remember your actions or preferences over time.

You can delete all cookies that are already on your computer, and you can set most browsers to prevent them from being placed. However, if you do this, you may have to manually adjust some preferences every time you visit a site, and some services and functionalities may not work.

Most browsers support cookies, but you can set your browser to decline them and can delete them whenever you like. You can find instructions here for how you can do that on various browsers.

This website uses cookies to:

  1. Identify you as a returning user and to count your visits in traffic statistics analysis
  2. Remember your custom display preferences (such as light/dark theme option)
  3. Provide other usability features, including tracking whether you have already given your consent to cookies

Enabling cookies is not strictly necessary for the website to work but it will provide you with a better browsing experience.

The cookie-related information is not used to identify you personally and is not used for any purpose other than those described here.

There may also be other types of cookies created after you have visited this website. This site uses Google Analytics, a popular web analytics service that uses cookies to help to analyse how users use the site. The information generated by the cookie about your use of this website (including your IP address) will be transmitted to and stored by Google on servers in the United States. Google will use this information for the purpose of evaluating your use of other website, compiling reports on website activity, and providing other services relating to website activity and internet usage. Google may also transfer this information to third parties where required to do so by law, or where such third parties process the information on Google’s behalf. Google undertakes not to associate your IP address with any other data held by Google.