More results...

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

Class Register

class-register

Learning Objectives


In this challenge we are going to focus on accessing a text file in Python to:

  • Read the content of the file line by line,
  • Write data to a new file.

Before completing this challenge, you should read about the main file handling operations using Python.

Your Challenge


Your challenge consists of writing a Python script that could be used by a teacher to take the register at the beginning of the lesson.

Your program will need to:

  1. Read the text file called classList.txt, line by line.
  2. For each student (line of the text file) the program should ask the teacher if the pupil is pesent (“/” code), absent for medical reasons or illness (“M” code), on a school trip (“T” code), or absent with no reason provided yet (“X” code).
  3. The program should store the teacher’s input alongside with the name of each pupil into a new text file called “register.txt”.

To complete this challenge you will need to use the class list text file:


TextFile
ClassList.txt

Python Code

Complete the Python code below…

unlock-access

Solution...

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

Times Table Challenge

x-times-table-challenge

Learning Objectives


By completing this challenge you are going to perform arithmetic operations in Python. You will also learn how to manipulate counted loops (for loops) and how to combine loops together using nested loops.

Times Table


Look at the code below. It’s asking the user to type a number and then it displays the times table for that number using a for loop.

Nested Loops


When you insert a loop within a loop, you are using “nested loops”.
Check the code below that uses a nested loop.

Challenge #1


Tweak the code above to display a full times table:
times-table

Challenge #2


Tweak your code to display a number grid as follows:
number-grid
unlock-access

Solution...

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

Higher or Lower Game

Higher-Or-LowerFor this challenge you will design and write a program to play against the computer.
The computer will display a random number between 1 and 1000. It will then ask the end-user whether they believe the next number will be higher or lower. The program will then generate the next number. If the user guessed right (e.g. the next number is higher or lower than the previous one) then the user scores one point. The game stops when the user guesses it wrong.

Learning Objectives


By completing this challenge you are going to use selection (IF statements) and iteration (While loop). You will use comparison operators such as > , < and == to compare numbers. You will use variables to store the value of random numbers, retrieve user input and keep and increment a score as the game progresses.

Complete the Python Code


Molecular Mass Calculator

molecule

Learning Objectives


In this challenge you will improve your string manipulation techniques as well as use Python to perform some basic mathematical calculations.

Did you know?


The molecular weight (mass) may be calculated from the molecular formula of the substance; it is the sum of the atomic weights of the atoms making up the molecule. The molecular mass is expressed in atomic mass unit (amu).

For example, water has the molecular formula H2O, indicating that there are two atoms of hydrogen and one atom of oxygen in a molecule of water.

Atom Atomic Mass Quantity Total Mass
H 1.008 2 2.016
O 15.999 1 15.999
H20 18.015

In the following script we use a Python dictionary called atomicMass to store the atomic mass of some of the main atoms (C, H, O…)

We then work out the molecular mass of water: H20.

Your Challenge


Adapt this script to get the program ask the end-user to enter a molecular formula of their choice.
Using string manipulation techniques your Python script will break down the formula entered by the end-user and calculate the total mass of the molecule.

Test your script with the following molecules:

Molecule Formula
Water H2O
Carbon Dioxide CO2
Methane CH4
Butane C4H10
Carbonic Acid H2CO3
Acetone CH3COCH3
Lactose C12H22O11

Challenge #2


Add more atoms to the atomMass dictionary as follows:

Atom Atomic Mass
Fe 53.939
Ag 106.905
Cu 62.929
Na 22.989
Cl 34.968

Adapt your script so that it recognises when these atoms are used by the end-user when entering their molecular formula. This will be based on the use of uppercase & lowercase letters.

Test your script with the following molecules:

Molecule Formula
Sodium Chloride NaCl
Iron Oxide Fe2O3
Copper Chloride Hydroxide HOCuCl
unlock-access

Solution...

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

My Conversion Library

conversion

Learning Objectives


In this challenge you will learn how to create your own library and how to use it in your programs.

A library is a collection of functions and procedures. A library can then be reused in all your programs using the import instruction.

You have most likely used some libraries in the past, for instance the random library or the maths library.

We are going to create our own library focusing on conversion formulas. It will be useful when we need to convert for example distances from one unit to another (e.g. miles to km, cm to inches).

We have started the library for you by defining two functions: kmToMiles() and milesToKm().

Your Challenge


Complete the library to add the following conversion functions (Roll-over the frames below to reveal the conversion formula):

conversion-distanceConverting Distances

conversion-miles-to-km-1
conversion-inches-to-cm-1
conversion-temperatureConverting Temperatures

conversion-celsius-to-fahrenheit-1
conversion-weightConverting Weights

conversion-kg-to-lb-1
conversion-timeTime Conversion

conversion-seconds-1
unlock-access

Solution...

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

Lightning Distance Calculator

lightning-pictureHave you ever seen a lightning flash or heard the thunder of lightning and wondered how close you were from the ligthning strike? Have you noticed that there is always a delay between the flash of light and the clap of thunder when a lightning occurs?

It is possible to calculate the distance to a ligthning strike by counting the seconds between the lightning flash and the sound of thunder.

When lightning strikes, the first thing you see is the flash of light which you can see instantly. This is because light travels at a very high speed (Speed of light = 300,000 km/s). At the same time a clap of thunder is created. However, in the air a sound wave does not travel as fast as light, so it may take a few seconds for this clap of thunder to reach you. This depends on how far you are from the lightning.

Look at the following diagram and check the formula of speed. Bear in mind that the speed of a sound wave in the air is 340 m/s.
lightning-distance
speed-distance-formula

Your Challenge


Write a Python program that asks the end-user to enter a number of seconds. The program should output the distance (in meters or kilometres) from the lightning strike.

Note that 1 mile = 1.609 km, you may want to convert this distance in miles.

unlock-access

Solution...

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

Space Mission

Rosetta: Rendez-vous with a comet


Rosetta is a robotic space probe built and launched by the European Space Agency. Along with Philae, its lander module, Rosetta is performing a detailed study of comet 67P/Churyumov–Gerasimenko.
Rosetta_spacecraft
Rosetta
Philae_lander
Philae
Comet_67P
Comet 67P

Rosetta: © (CC) ESA/ATG medialab – https://www.flickr.com/photos/europeanspaceagency/11206647984/
Philae: © (CC) ESA/ATG medialab – https://www.flickr.com/photos/europeanspaceagency/10796307373/
Comet 67P: © (CC) SA/Rosetta/NAVCAM – http://blogs.esa.int/rosetta/2014/09/19/cometwatch-19-september/

Your Challenge


At the time this blog post was created, the European Space Agency just received new photographs of comet 67P taken by Rosetta. The spaceship was 500 million km away from Earth.

Write a Python script to answer the following question:

Considering that electro-magnetic signals used to communicate with Rosetta travel at the speed of light (300,000 km/s), how long did these pictures take to travel from Rosetta to planet Earth?

To help you solve this problem you may need to investigate the following formulas:
speed-formula
You will need to make sure your script gives the answer using in the correct format: hh:mm:ss.

Challenge #2: Time travelling?


Did you know that when you look at the stars in the night sky you are time travelling. You are in fact looking at what the stars did look like years and years ago! This is because the stars are extremely far away from planet Earth, and the light they create takes several years to reach us.

When astronomers measure the distance of stars (from planet Earth) they do not use the kilometer (km) unit but instead they use the light-year unit. One light-year represents the distance that light travels in one year, at the speed of light (300,000 km/s).

Your challenge consists of writing a Python script to calculate the distance in km of one light-year.

Then you will use your script to calculate the distance in km for the following stars:

Star Distance from Earth (in light-years)
Alpha Centauri 4.24 light-years
Barnard’s Star 5.96 light-years
Luhman 16 6.59 light-years
WISE 0855-0714 7.2 light-years
Wolf 359 7.78 light-years

Source:
http://en.wikipedia.org/wiki/List_of_nearest_stars_and_brown_dwarfs

Discount Price Calculator

discountShopping during the sales can sometimes be very confusing. With discounted prices at 10%, 20%, 50% or even 70%!

For this challenge you are going to write a Python script that prompts the user to enter a price in pounds (or in your own currency) (e.g. £90) and a discount rate to apply (e.g. 20%).

Your program will then calculate and display the discounted price.

Video Tutorial


Python Code


Complete the code provided below:

Testing


Once your code is done, complete the following tests to check that your code is working as expected:

Test # Input Values Expected Output Actual Output
#1 Price: £100
Discount Rate: 25%
Discount: £25
Discounted Price: £75
#2 Price: £160
Discount Rate: 40%
Discount: £64
Discounted Price: £96
#3 Price: £180
Discount Rate: 10%
Discount: £18
Discounted Price: £162
unlock-access

Solution...

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

Fahrenheit to Celsius Converter

Fahrenheit-CelsiusDegree Fahrenheit (°F) and Degree Celsius (°C) are the main two units to measure temperature.

The Fahrenheit scale is used mainly in the USA whereas other countries tend to use the Celsius scale.

It is possible to convert a temperature from Celsius degrees to Fahrenheit and vice-versa using the following conversion formulas:
fahrenheit_to_celsius_formulas

Your Challenge


Write a Python script to:

  • Ask the end-user whether they want to convert from Fahrenheit to Celsius or from Celsius to Fahrenheit,
  • Ask the user to enter a temperature in the correct unit,
  • Apply the conversion formula and display the temperature in the new unit.

Testing


You can now use your code to complete the following test plan to test your code:

Celsius degrees to Fahrenheit conversions:

Test # Input Values (°C) Expected Output (°F) Actual Output
#1 20 68
#2 -10 14
#3 0 32

Fahrenheit to Celsius degrees conversions:

Test # Input Values (°F) Expected Output (°C) Actual Output
#1 68 20
#2 14 -10
#3 32 0

Challenge #2


On the temperature scale, the 0°C (=32°F is called Freezing Point which is when water starts freezing and becomes ice. At 100°C (=212°F) water starts to boil. This is called Boiling Point.

Adapt your script to write a message to the end user as follows:

  • You are above boiling point for all temperatures above 100°C
  • You have reached boiling point if the temperature is exactly 100°C
  • You are between freezing point and boiling point for all temperatures between 0°C and 100°C
  • You have reached freezing point if the temperature is exactly 0°C
  • You are below freezing point for all temperatures below 0°C
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 legend of the chessboard

chessboardThe story tells us that long long ago the great Sultan of India loved to play games. As he started to get bored of the games that were present at the time he asked a wise man who lived in his kingdom to come up with a new game.

This, according to the legend, is how the game of chess was invented. The Sultan was very pleased with the wise man who had invented the game and in return he offered the wise man a reward of his own choosing.

riceTo the Sultan’s surprise, the wise man did not ask for any gold. Instead he only had one request: He asked just for a few grains of rice in the following manner: the sultan was to put a single grain of rice on the first chess square, two grains on the second square, four on the third square and carry on doubling this number on every consequent square to fill up all 64 squares of the chessboard.

This seemed to the ruler to be a modest request, so he called for his servants to bring the rice. Did the Sultan manage to fulfil the wise man’s request?

Python Code


Your task consists of writing a python script to calculate the total number of grains of rice required to cover the chessboard following’s the wise man’s request.

unlock-access

Solution...

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