More results...

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

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: , , ,

Flags of the World

We are trying to build a program that will ask the end-user to choose a flag amongst six flags. The program will then try to guess the selected flag by asking a maximum of two questions.

Look at the flowchart below used to describe our algorithm: (Click on flowchart to open in new window)
flags of the world flowchart

Challenge #1


Analysing this flowchart, can you guess the country corresponding to these six flags? (Type your answer below each flag).

Flag-Tanzania Flag-Turkey Flag-Luxembourg
Flag-Ghana Flag-Sweden Flag-Chad

Challenge #2

Using Flowchart Studio, design a similar flowchart used for the computer the guess the correct flag amonst these six new flags. You will need to ask different questions.

Flag-Jamaica Flag-China Flag-Iceland
Flag-Colombia Flag-Cuba Flag-Denmark

Challenge #3


Can you tweak the algorithm from the previous task to get the computer to guess the correct flag amongst all twelve flags, using a maximum of three consecutive questions?

Tagged with: ,

Fireworks Display

fireworks

Coding is an Art


Let’s first whatch this video clip which shows how a computer program can be used to create a large scale, live and interactive fireworks display:
Watch video clip

Fireworks Code


Inspired by this application of computer science, we have decide to create our own firework display using a Python script.

For this challenge we are using the processing library which is used to draw on the screen and to refresh the screen based on a frame rate. This is what we need to create our fireworks animation, based on a 24 frames per second.

Your task:


Analyse the code above. Change some of the parameters to see the impact on the aimation. Customise your fireworks further based on your preferences.

Interactive Fireworks


Let’s make an interactive fireworks display that responds to the user typing on the keyboard.
To test this script make sure you click on the fireworks display first (while it’s running). Then you will be able to interact with it by pressing any key on the keyboard:

Tagged with: , ,

My daily routine

daily-routine

Learning Objectives


When you write lines of code, there are three ways you can control the order these lines will be executed by the computer:

  1. Sequencing: This means that the computer will run your code in order, one line at a time from the top to the bottom of your program. It will start at line 1, then execute line 2 then line 3 and so on till it reaches the last line of your program.
  2. Selection: Sometimes you only want some lines of code to be run only if a condition is met, otherwise you want the computer to ignore these lines and jump over them. This is achieved using IF statements. e.g. If a condition is met then lines 4, 5, 6 are executed otherwise the computer jumps to line 7 without even looking at line 4,5 and 6.
  3. Iteration: Sometimes you want the computer to execute the same lines of code several times. This is done using a loop. There are three types of loops: For loops, while loops and repeat until loops. That’s handy as it enables you not to have to copy the same lines of code many times.

Let’s Get Coding


For this task we are going to write a program that uses sequencing, selection and iteration all in the same program! Our program will be used to print our daily routines on screen over a full week. Look at the code below and try to make sense of how it works. Can you spot where and how sequencing, iteration and selection are used in this code?

Your Task


Your task is to tweak this code to customise your daily routines. Add to it some of the activities or clubs you go to. For instance:

  • Are you going swimming on Tuesdays?
  • Do you play football on Saturdays?
  • Do you go to a Youth Club on Thursdays?
  • Do you play music on Fridays?
  • Is Friday Fish and Chips day?
Tagged with: , ,

Sweet Shop

sweetsHave you ever been in a sweet shop to buy sweets? For this challenge we are going to spend £5 in a sweet shop hence we need to find out how many sweets we can afford. We will want to pick and mix sweets until we have spent all our money.

To help us buy our sweets we are going to write a program that will help us decide how many sweets we can afford while allowing us to pick and mix different types of sweets.

Here are the sweets available in the shop:
sweet-shop-price-list

Here are the main steps of our program which will:

  1. Display a price list of all the sweets available in the shop,
  2. Ask the end-user how much they would like to spend,
  3. Ask the user which sweet they would like to buy and how many of them they would like (A to E),
  4. Allow the user to enter X (instead of the A to E letter for a sweet) to stop buying more sweets,
  5. Check whether the use can afford these and if they can, calculate and display how much money they have left,
  6. Repeat steps 3 to 5 for as long as the user has some money left.

Now let’s see how a flowchart can help us describe these steps further:
sweet-shop-flowchart

Your task:


Use the above flowchart to complete the code below:

unlock-access

Solution...

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

The window cleaner’s quote

window-cleanerA window cleaner uses the following pricing policy to calculate how much to charge for cleaning all the windows of his customer’s dwelling. This pricing policy is based on the number of windows that need to be cleaned and works as follows:

  • All quoted prices include a fixed call out fee of £10,
  • Then, the first five windows are charged at £2 each,
  • The next five windows are charged at £1.50 each,
  • Any additional windows are charged at £1 each.

Your task is to write a computer program that prompts the end-user to enter the number of windows of their dwelling. The program will then calculate the quoted price using the pricing policy described above and display it to the end-user.

Test Plan


Now that you have implemented the cost calculator for the house cleaner, you are going to test your code by completing the following tests: see test plan below. For each test compare the outcome of your program with the expected outcome. If these are different then you’ll need to review and tweak your code.

Test # Input values Expected outcome Actual outcome
1 Number of windows: 3 Cost: £16
2 Number of windows: 5 Cost: £20
3 Number of windows: 7 Cost: £23
4 Number of windows: 9 Cost: £26
5 Number of windows: 10 Cost: £27.50
6 Number of windows: 11 Cost: £28.50
7 Number of windows: 13 Cost: £30.50
8 Number of windows: 15 Cost: £32.50

Extension Task:


At the beginning of the year, the house cleaner wants to attract new customers by offering a 10% discount to all quoted prices.

Adapt your code to include a 10% discount in the quoted price.

Solution



unlock-access

Solution...

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