Python Programming Challenges
If you’ve been through this website from start to finish, you’ll have learnt the Python programming language, and (more importantly) used the language to solve a number of problems.
However, you have used a python feature to solve a problem straight after learning how to use it. For example, you made an inventory program straight after I told you what a list was! One important skill for any programmer is to decide for themselves how a problem should be solved. That skill takes time to develop… and that’s what these challenges aim to help with. You can solve any of these challenges using any feature of Python that you know about!
These challenges shouldn’t really be attempted until you’re comfortable using Python, as little help will be given with the solutions. You should design, code and test as usual.
Variable Length Banner
Right at the start of these tutorials, we wrote what you’d probably now consider to be a very simple program to print a banner of someone’s name. It probably produced output like this:
The trouble is, it probably sometimes looked like this:
.::Christopher Thompson III::.
Make a banner program that fits the banner around the user’s name, like these lovely examples:
* TJ * * Christopher Thompson III *
The player enters 4 numbers, and the computer tells the player how many (but not which) of the 4 numbers are correct.
Here’s an example game output:
Guess the 4 numbers in as few tries as possible
Well done... That took you  attempts!
You could improve this game by letting the user choose a difficulty rating, and by saving the high score/name (to a variable or a file).
Just to clarify, you’re not creating a mediocre calculator, but a program for calculating averages.
The user should be able to enter a series of numbers, and the program should print the average of these numbers. You can use floating point number variables, or store the input in a list.
Your program might be used to calculate average temperatures for a week, or a batting average for a cricket team, among others. You could even expand the program to print the mean, median and mode averages.
When registering on a website, you sometimes need to give a valid email address. If a user enters an invalid email address, the website should alert the user to their mistake, and not allow them to register until it is corrected.
Write a program to let the user know whether the email address they entered was valid or invalid.
Examples of valid email addresses are:
Examples of invalid email addresses are:
- kelly firstname.lastname@example.org
You could also provide a helpful warning, such as the following:
Enter your email address: email@example.com.
*Did you mean firstname.lastname@example.org [y/n]
You should create a program to display a bingo board starting with 10 randomly chosen numbers, like this:
You’ve written programs using random numbers before, but have never needed to store more than one. The problem with just doing this…
listOfNumbers = 
for n in range(10):
listOfNumbers += [random.randint(1,101)]
…is that you may get the same number more than once. So how can we make sure that we get 10 different numbers?
One way is to create a list of all of the numbers from 1 to 100, shuffle the list, and then take the first 10 items of the shuffled list:
#this creates a list of the numbers 1 to 100
possibleNumbers = [i for i in range(1,101)]
#this shuffles the list
#this gets the first 10 numbers in the list (0-9)
board = possibleNumbers[:10]
You could create a fully functional Bingo game, where the user is presented with a board, and types in numbers that are called. If the user has a called number on their board, the number could be removed from the list and the board redrawn. You could also create another program for the caller, to generate the numbers.
Password Reset Program
A student should be able to use this program to change their password to the school network.
The user should enter the new password twice (why?). Your program should display “Password change successful” if the new password:
- Has at least one capital letter;
- Has at least one lower-case letter;
- Is at least 8 characters long.
You can also improve on this program in one (or both) of 2 ways:
- You could have a file with usernames and passwords. The user could enter their username, and only be able to change their password if they successfully enter their current (old) password. Your program could then save the new password to the file. This could also work for multiple users!
- You could save the passwords in the file securely by encrypting them. The following code will help you with this:
#get a character
charIn = input(">")
#find the numerical number representing the character
num = ord(charIn)
#encrypt it by changing the number by 3
num += 3
#create an encrypted character and print it
When making websites, one way of thinking about different colours is by their ‘RGB’ values. RGB stands for Red, Green and Blue. Every colour can be ‘made’ by combining different amounts of Red, Green and Blue. Each part of the colour has a value between 0 and 255. Here are some examples:
(255, 0, 0) = RED
This is a colour made up of ‘full’ red, and no green or blue. This makes red.
(0, 139, 139) = TEAL
This is a colour made up of no red, and some green and blue. This makes teal.
Another way of representing colour is by a hexadecimal code. This is the same RGB combination, but with 2 hex digits (00 – FF) representing each or the three component colours:
#FF0000 = RED
#008B8B = TEAL
Write a program to convert from a decimal RGB colour code to its hexadecimal code equivalent.