A common syntax error in Python is when brackets and quotation marks are opened but never closed ( ) { } [ ] “ “. Can you find somewhere in this code where that has happened? Find it and fix it.
What is your first name: What is the name of your school:
Can you find and fix all the “bugs” in these Python programs?
We’ve created some small programs written in Python, one of the most popular computer programming languages in the world. But each program has at least one “bug” in it.
What’s a bug?
In computer programming, a bug is a mistake in the code. Sometimes the bug might be easy to fix (like a typing mistake or a misspelling). Other times it can be trickier to spot.
Your task is to find and fix the “bugs” that are stopping these programs from working.
How to do it
For each task, start by running the Python program by using the ▶ Run button. At the right hand side you’ll see the output from the program. This is where you’ll also see any error messages from the Python interpreter.
Error messages can give software developer hints as to what’s gone wrong. You should make sure you read any error messages carefully as they’ll often tell you the type of error, and point you towards a line in the code. That’s the place where you’ll find the mistake.
Take the hint
For each task there are some instructions above the code. It tells you what the program should be doing. Remember to read these instructions as it’ll give you a clue as to what you need to be looking for.
Your challenge is to find the bugs and fix them
If you’ve got good attention to detail and are great at fixing problems, you’ll be brilliant at these challenges. These challenges are based on real-life challenges that software developers face every day. This is a chance to get hands on with Python code. Now it’s time for you to hunt those bugs!
Can you find the mistake in this code?
This code should welcome a user called Sally using their name, it should output “Hello Sally”. But, it’s not working! Run the code and look at the error message on the right hand side.
This is a NameError in Python. This type of error happens when you try to use a function or variable name that doesn’t exist. In this case, it’s caused by a typing mistake. Can you fix it?
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
Try running this code, it should tell all of the drivers to start their engines and go. This code has an “invalid syntax” error. That means the code isn’t following the rules of the language. Just like English, Python has grammar and rules that must be followed for it to make sense.
A common syntax error in Python is when brackets and quotation marks are opened but never closed ( ) { } [ ] “ “. Can you find somewhere in this code where that has happened? Find it and fix it.
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
In this example the code runs without any errors, but the output isn’t as expected. Can you see what the problem is? Can you fix it?
It should inform the customer that their order is on the way, telling them their order number, and the date of dispatch.
Try changing the code on line 15.
It looks like the variables for shipping_date and order_ref might be in the wrong order.
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
This program should let the user type in a whole number and then tell them if it is odd or even. Try running this code. This time you’ll get an “Syntax error - unmatched”. The error message shows you what line the problem is on. Can you work out how to fix it?
Remember brackets always need to be in pairs - with an opening bracket and a closing one.
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
This program is meant to send out a newsletter to a mailing list. Try running the code. This time you’ll get an “Indentation error”, another type of syntax error. This happens when the code hasn’t been formatted correctly, the Python interpreter doesn’t understand it.
To solve this error you need to make sure the code is laid out correctly. You need to add four additional spaces at the start of line 12.
It should look like this:
if answer == "y":
send_newsletter()
else:
print("OK. The newsletter is not sent")
exit()
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
Try running this code for a smart speaker app. When the smart speaker receives a voice command, it downloads the latest news update and then plays it on the speaker. However, when you run the code you’ll notice an error message.
This looks like a logic error, another common type of bug. In this case there is nothing in the code that breaks the rules of Python, instead the problem is the program isn’t doing what it’s expected to do.
Some logic errors happen when the instructions are in the wrong order. Read through the order of the instructions carefully. Can you work out which instruction is in the wrong place? Find it and fix it.
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
Try running this code. This is an app used by an electric vehicle (EV) to let the driver know if they’ve got enough battery charge left to get to their destination. It compares how far the EV is from the destination with the range of the EV (how far it can go on the remaining battery).
Try running the code and read the results. Something isn’t quite right. The destination is further than the range, and yet it says there is enough charge to reach the destination. This looks like another example of a logic error. Can you find it and fix it?
Take a look at line 9. At the moment, it checks if the distance to the destination is greater than (or equal to) the current range. Is this right?
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
Users log in to their bank account using this app. However, the security team has identified that people have been able to access accounts using the incorrect password.
First, run the app and try entering using valid credentials. Username “John” and password “grumpy elephant oliver” (without the quotes). It should let you in.
Then, run it again using a valid username “John” but an incorrect password. Does it still let you in?
Look at the code carefully. Is there something wrong with the order of instructions? This might be an example of unreachable code: where a line of code is never going to be run. Can you find the bug and fix it?
The code on line 14 is meant to check the user’s password, however the code on line 13 grants the user access before their password is checked! These are the wrong way around. This is a serious logic error, as the app grants the user access before checking the password is correct.
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “solved token” code. Copy and paste or type in into the “Solved Token” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem:
You’ll need to use all your bug hunting skills, and some logical thinking, to solve this last puzzle.
Sorry, no hints this time 😆
Once you’ve fixed the bug. You need to send us the evidence you’ve solved it.
When your code runs correctly you’ll be given a “secret word” code. Copy and paste or type in into the “Secret Word” field below.
If you want to share your code with us, you can copy and paste the URL for your code into “Share link”.
So how did you do it? Type a message here to tell us how you solved the problem: