05: Python: Naming Conventions

Try it:

Use variables and constants with sensible names
Python naming conventions: Use variables and constants with sensible names

Below you’ll see some example python code that draws some shapes

Press Ctrl + Enter  to run the code.

Click on your score in the top right corner of the code editor to see what your code needs to do.

Any line of code that starts with a #  is a comment: it doesn’t affect how the program runs, it’s just there to help explain what the code does.

There’s lots of code here that you probably won’t understand yet. Don’t worry: all of the challenges just involve changing the variables and constants that you see on the first 11 lines.

Challenges:

  1. Try changing the number of shapes constant to 6

    You’ll see a constant called NUMBER_OF_SHAPES on line 5. See what happens when you change it.
    You’ll see this constant used elsewhere in the code. This is why constants are useful – you set their value once at the top of your code and then you can use that number lots of times elsewhere in your code.

  2. Change the line_colour variable so that it’s always set to “red”

    Replace the input("...") code on line 10 with "red".
    This means that line_colour never changes when your code runs – it’s always set to "red". This means we should probably change it to a constant instead of a variable…

  3. Change the line_colour variable into a constant called LINE_COLOUR

    This is a little harder than just changing line_colour into capital letters on line 10 because you’ll have to change the rest of the code whenver it says line_colour to say LINE_COLOUR instead.

Notice that python doesn’t actually complain if your code tries to change the value of a constant (unlike other programming languages).

Putting the name of a constant in capital letters is just for your benefit (and anyone else who reads your code) to tell them that that value shouldn’t change while the code runs.

On the next page you’ll get some code examples that have been deliberately broken for you to try to fix.

One thought on “05: Python: Naming Conventions

Leave a Reply