09: Selection in Python

Debug it

Tell your code how to make decisions
Python Selection: Tell your code how to make decisions

Below is some code that has been deliberately broken so that you can fix it. 

There aren’t any syntax errors or runtime errors but the code doesn’t do what the comments say it should do

Challenges

  1. Fix the syntax error on line 7

    In python you use one = to set the value store in a variable.

    If you want to test if two values are the same, you need to use ==

  2. At the moment Q2 only appears if you get Q1 wrong. Make it so Q2 always appears.

    Indentation is really important in python. Any code that is indented under an if statement will only run if that condition is met.

  3. Find and fix the logical error

    There’s actually two logical errors on the same line. 

    A logical error means that your code does what you’ve told it to do, but you’ve told it to do the wrong thing.

    Here, our code compares the string value stored in answer to Q2 with the integer value 40. That’s not right.

    Think what value and data type answer should be compared to instead.

On the next page you’ll get some ideas for extended projects to apply what you’ve learnt.

Leave a Reply