Lesson 3 – Comment Code, Maths Quiz and 12x Tables Quiz

Learning Outcomes

  • Demonstrate and explain the practice of commenting code
  • Create a simple maths test
  • Explain the need to convert a string to an integer
  • Make use of ‘if’ statements to create a 12 times table test

Assessment Criteria

All students must (Developing):

  • Create a quiz with a least 3 questions
  • The code must have a header comment with student’s name, tutor group, date and description of what the program does
  • Appropriate feedback should be given to the user

Most students should (Meeting):

  • Create a quiz with at least 8 questions
  • The code must have a header comment with student’s name, tutor group, date and description of what the program does
  • Appropriate feedback should be given to the user
  • There should be some screenshots showing that the program code has been tested

Some students could (Exceeding):

  • Create a quiz with 12 questions
  • The code must have a header comment with student’s name, tutor group, date and description of what the program does
  • Appropriate feedback should be given to the user
  • There should be some screenshots showing each question being answered correctly and incorrectly to prove that the program works properly

Keywords

Words to learn: comments, interpreter, indents, header

Starter – Recap

In the last lesson you created a program that asks the user some questions and then replied in an artificially intelligent way.  Open up your program and add the following question to it:

“Where have you been on holiday?”

The computer should reply with “Hmmm… holiday_place sounds like a nice place to go!” (but with the name of the place the person has typed instead of holiday_place)

Main – Commenting Code

Programmers can read program code, but it’s not always easy to work out what a piece of code does!  Therefore most programming languages allow you to make notes which are known as comments.  A comment is ignored completely by the interpreter which turns your Python code into machine code that the computer can understand.  Your teacher will show you how to add comments to your code – try adding them to your question asking program.

After you have done this, answer the following questions as a class:

  • What information do you think other people would find useful in a comment?
  • Where could you put this information?
  • Is it necessary to comment on every single line?
  • If the interpreter ignores everything after the # symbol, how else could commenting be useful?

Main – Creating a Maths Quiz

Load up Python by going to the Start Menu->Python 3.3->IDLE.  When the Python Shell appears go to the File menu and choose New Window.  A blank document will appear.  Type in the script below exactly as it appears.  It is a picture, so you can’t copy and paste!

yr7_term4_lesson3_img1

Once you have typed it in, save the program as maths_question.py – then go to the Run menu and choose Run Module.

If you get this working, try adding some more questions in!

As a class, answer the following questions:

  • answer = int(answer) – thinking back to use of integers last lesson, what does this do and what does int mean?
  • If answer == 4 – What does the == translate to in plain English?
  • What does the ‘else’ translate to in plain English?
  • Why are the indents necessary after the if and else statements?
  • What happens if the colons are not there after the 4 or else?

Mini Assessment – 12x Table Quiz

To check that you understand what we have done up until this point, attempt the following task.  Create a test that will check the user’s knowledge of the 12 times multiplication table, with between 4 and 12 questions. It must be saved with the name 12_times_table.py and it must include a header comment, with (at least) a description, your name and tutor group and today’s date. The program must feature some other use of comments and must work successfully.

Once you have finished, print out your code and put it into your Computing folder.  To reach the higher assessment criteria, take some screenshots to prove your quiz works.

Homework

Complete the floor polisher exercise.