More results...

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

Resistor Value Calculator

Resistor Value Calculator

resistor-5When designing electronic circuits you may need to use resistors. Each resistor has a resistance value expressed in ohms (Ω). Resistors are identified by a series of concentric coloured bands. Each band colour has a value which is used to calculate the resistance value of the resistor.

Here is how the resistor colour code works:
Resistor-Color-Bands

Resistor-Color-Codes

You can check the resistance value of a resistor online using an online calculator.

Your Challenge


Your challenge consists of writing a program that asks the user to enter three colours (e.g. Red, Red, Green) and returns the resistance value (e.g. 2,200,000 Ω or 2,200 kΩ).

Test your code


Test your code to find the resistance value of the following three resistors:

Resistor1

Resistor2

Resistor3

Challenge #2: The other way round


Create a computer program that asks the user for a resistor value in ohms and returns the three colours matching this value.
unlock-access

Solution...

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

Meet E.V.E.

eveMeet E.V.E., a computer algorithm with artificial intelligence. E.V.E. can communicate with human beings. Well it’s only a start and we need you to complete the code provided below for E.V.E to be able to communicate more effectively with teenagers.

Learning Objectives


By completing this programming challenge you will learn how to code in Python. You will use the input() method to ask questions to the end user. You will store their answers using your own variables. You will use if statements to check the user answers and the print() method to write information on the screen.

Your Challenge

Complete the code below to let E.V.E. ask more questions to the end-user.


unlock-access

Solution...

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

Optical Illusions

illusion-1

Which of the above two purple circles is bigger?

Learning Objectives


In this challenge you will use Python Turtle to draw geometric shapes and optical illusions.

To help you we have created three functions as follows:

  • drawLine() to draw a straight lines between two points,
  • drawSquare() to draw a square of a given size and colour,
  • drawCircle() to draw a circle of a given size and colour.

To position these shapes on the screen you will need to use x,y coordinates and pass these coordinates when using the three functions given above.

(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:
xy-coordinates

Check the code


Your Challenge

Complete the code given to draw the following optical illusions:

illusion-1

Which of the above two purple circles is bigger?

illusion-2

Which of the above two lines is longer?

illusion-3

Which of the above two lines is longer?

illusion-4

Are both purple lines perfectly straight?

illusion-5

Are all the white dots really white?

illusion-6

Tagged with: ,

All Stars Football Team

football2In this challenge we are looking at how a list can be used to store all the players’ names of a football team.

Using a set of functions we then manipulate this list to:

  • Find a value in a list and change it to something else. We use this to perform a substitution: replace a player with another player.

  • Find a value in a list and remove it from the list. We will use this to issue a red card and hence remove a player from the team.

When using our functions we will pass some parameters such as the name of the players we want to substitute or the name of the player receiving a red card.

To make the code easier to follow we created a football.py file that contains three functions:

  1. displaySquad()
  2. substituePlayer()
  3. redCard()

Our main program first initialises the team as a list of 11 players called allStars.

Then we use the substituePlayer function and redCard function to change the composition of the team.

Finally we use the displaySquad function to display the full team at the end of the game.

Check the code

Your Challenge


Tweak this code to change the composition of the allStars team using your favourite selection of 11 players.

Perform some substitutions and issue a few red cards to see how it will affect your team.

Tagged with: , , , ,

Download Python

Before installing Python check our recommendations at the bottom of this page!

python-logoReady to complete some of these Python challenges at home? Then you will need to download Python 3 which is completely free to download: https://www.python.org/downloads/.

We also recommend you to install a Python IDE such as PyScripter which is also free to download: https://code.google.com/p/pyscripter/. It makes coding and debugging a lot easier.

Download Python Scripter

Our Recommendations


To avoid any incompatibility issues, for Windows users we recommend you to install the following versions of Python, PyScripter and Pygame:

Python version 3.9 Download (Source: https://www.python.org/downloads/windows/ More recent versions may not be fully compatible with our recommended versions PyScripter / Pygame
PyScripter Version 2.6 https://sourceforge.net/projects/pyscripter/
Pygame version 1.9.2a0 for Windows 32 bit – Python 3.3 Download (Source: https://bitbucket.org/pygame/pygame/downloads)

Should you not have the above versions, we would recommend uninstalling Python / PyScripter and Pygame to install the recommended versions instead.

Python Online


Should you decide not to install Python on your computer, you can still use online coding environments such as Trinket.io or CodingGround:

trinket
codingground

Tagged with: , , , ,

Lottery Numbers

lottery-numbersIn this challenge we are going to write a Python program that automatically generates six random numbers (from 1 to 50) and display them on the screen, sorted in ascending order.

The program will need to make sure that each number is unique; the same number cannot come twice in the selection of six selected numbers.

Learning Objectives


By completing this code, you will understand the difference between the two types of loops used in Python:

  • For Loops (Count-Controlled Loops): A count-controlled loop is one that is executed a certain number of times. In our example, because we know that we need exactly six numbers we are going to use a for loop to repeat (iterate through) some of our code exactly six times.
  • While Loops (Condition-Controlled Loops): A condition-controlled loop such as a While loop keeps going while a certain condition is true. As soon as the condition becomes false, the loop stops iterating. In our case, when generating a random number, we are going the check that the new number has not already been picked up. If it has then we are going to generate a new one and will carry on doing so till we get a number that has not already been picked up.

Check the code

Your Challenge

  1. Complete this code to store your own selection of six numbers. For testing purposes, you can store these numbers directly into the code and display these to the end-user. Alternatively you can ask the end-user to type six numbers.
  2. Then your code will use the program given above to generate the six lottery numbers.
  3. Finally your code will check and inform the end-user of how many numbers the user guessed correctly by comparing the user’s six numbers with the six randomly generated lottery numbers!

Note


The probability of guessing several lottery numbers is very low. You may want to tweak your program to only pick up numbers between 1 and 20 (instead of 50). This will give you a better chance to guess some of these numbers and hence test whether your code is working.

Video Tutorial



unlock-access

Solution...

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

Fruit Machine

Learning Objectives


In this challenge you will complete a full Python Program to simulate a fruit machine.

You will use the random library to randomly select a fruit to display on screen.

You will use if statements to check if the user has won the Jackpot!

Your Task

Click on the picture below to see how the code works. Use this code to complete your own fruit machine.

fruit-machine

You can type your code using our online Python IDE:


unlock-access

Solution...

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

Python Basics

Python Cheat Sheet

Check the following Python instructions. These are all you need to know to start creating your first Python scripts!

You can also check our more advanced Python Helpsheet:
Python Cheat Sheet


Tagged with: , ,

Blackjack Challenge

blackjackIn this challenge you are going to create a blackjack game for one player. The computer will be the dealer.

Rules of the game


Source: wikipedia.

“Blackjack, also known as twenty-one, is the most widely played casino banking game in the world. Blackjack is a comparing card game between a player and dealer, meaning that players compete against the dealer but not against any other players. It is played with one or more decks of 52 cards. The object of the game is to beat the dealer, which can be done in a number of ways:

  • Get 21 points on the player’s first two cards (called a blackjack), without a dealer blackjack;
  • Reach a final score higher than the dealer without exceeding 21; or
  • Let the dealer draw additional cards until his or her hand exceeds 21.

The player or players are dealt an initial two-card hand and add together the value of their cards. Face cards (kings, queens, and jacks) are counted as ten points. A player and the dealer can count his or her own ace as 1 point or 11 points. All other cards are counted as the numeric value shown on the card. After receiving their initial two cards, players have the option of getting a “hit”, or taking an additional card. In a given round, the player or the dealer wins by having a score of 21 or by having the highest score that is less than 21. Scoring higher than 21 (called “busting” or “going bust”) results in a loss. A player may win by having any final score equal to or less than 21 if the dealer busts. If a player holds an ace valued as 11, the hand is called “soft”, meaning that the player cannot go bust by taking an additional card; 11 plus the value of any other card will always be less than or equal to 21. Otherwise, the hand is “hard”.

The dealer has to take hits until his or her cards total 17 or more points. (In some casinos the dealer also hits on a “soft” 17, e.g. an initial ace and six.) Players win if they do not bust and have a total that is higher than the dealer’s. The dealer loses if he or she busts or has a lesser hand than the player who has not busted. If the player and dealer have the same total, this is called a “push” and the player typically does not win or lose money on that hand.”

Final Product


You can check this game online.

Your Challenge


Your challenge is to recreate this game for one player only using your chosen programming language (e.g. Python, JavaScript…). The end-user will play against the computer (the dealer).

To start with you may want to create this game using a text-based interface. Cards could be displayed as follows:

  • ♠ As (Ace of spade), 2s, 3s, … 9s, 10s, Js, Qs, Ks
  • ♥ Ah (Ace of heart), 2h, 3h, … 9h, 10h, Jh, Qh, Kh
  • ♣ Ac (Ace of club), 2c, 3c, … 9c, 10c, Jc, Qc, Kc
  • ♦ Ad (Ace of diamond), 2d, 3d, … 9d, 10d, Jd, Qd, Kd

You may also want to investigate unicode characters to display the selected cards in a more visual way.

Tips / Questions to get you started


Before getting started you may want to ask yourself the following questions?

  • What data structure could I use to create the deck of 52 cards?
  • Once I have stored all 52 cards in my data structure, how can I shuffle the cards?
  • What variables will I need to store the initial amount ($) the player has, the value of the bet ($), the total number of points of the dealer, the total number of points for the player?
  • Input: What information do I need to ask/retrieve from the end-user and when does the program need to ask for that information?
  • Output: What information do I need to display to the end-user during the game? (and when in the game?)
  • When or how does the game end?
  • How can I break up this project into smaller achievable steps? What would be the first few steps that I would then focus on?

Complete the Code…


Graphical User interface


Should you decide to create a full graphical user interface (e.g. as a webpage using HTML & JavaScript or as a Python program using graphical library such as PyGame) then you can download the graphics for each of the playing cards from wikimedia.
playingcards
unlock-access

Solution...

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

Number Sequences

Learning Objectives


In this challenge we are going to apply our programming skills to perform some arithmetic operations. We will use arithmetic operators such as +, – and * as well as for loops to repeat instructions.

Context / Starter activity


A primary teacher likes to start her maths lesson by displaying a number sequence maths challenge on the board. Here are the last three challenges she has used with her class. Can you solve these?

Number Sequence #1:number-sequence-2
Number Sequence #2:number-sequence-3
Number Sequence #3:number-sequence-1

Number Sequences


There are different types of number sequences. Let’s investigate the most widely used types of number sequences.
You may want to read this page first: https://www.mathsisfun.com/numberpatterns.html

Arithmetic Sequences

An Arithmetic Sequence is made by adding the same value each time. When creating an arithmetic number sequence you have to decide of a starting number (e.g. “2”) and an increment (e.g. “3”)

number-sequence-diagram-1

Geometric Sequences

A Geometric Sequence is made by multiplying by the same value each time.
When creating a geometric number sequence you have to decide of a starting number (e.g. “2”) and a multiplier (e.g. “3”)

number-sequence-diagram-2

Square Numbers

1,4,9,16,25,36…

This sequence consists of calculating the squares of whole numbers.

number-sequence-diagram-3

Incremental Sequence

1, 2, 4, 7, 11, 16, 22, 29, 37, …

This sequence is generated by adding an “increasing” number, that is a number that each time is incremented by 1 as we progress through the number sequence. Not clear? Check the diagram below.

number-sequence-diagram-4

Fibonacci Numbers

1, 2, 3, 5, 8, 13, 21, 34, …

The Fibonacci Sequence is found by adding the two numbers before it together.

  • The 3 is found by adding the two numbers before it (1+2)
  • The 5 is found by adding the two numbers before it (2+3)
  • The 8 is found by adding the two numbers before it (3+5)

number-sequence-diagram-5

Python Code


Arithmetic Sequence

Triangular Numbers

Fibonacci Numbers

Challenge #1


Edit some of the above scripts to create other number sequences such as:

  • Arithmetic sequences using a different starting number and increment,
  • Geometric sequences,
  • Square numbers sequence.

You can also adapt or even mix these techniques to create your own number sequences!

Challenge #2


Adapt your scripts so that the program asks the end-user the question: “What number comes next in the sequence?” The user has to type their answer. The program should check if they have got the right answer or not.