Science Oxford Logo

CCC - Space Race

Resources for the 'Space Race' Creative Computing Club.

During this workshop, you will create a quiz program in Python, then investigate data from the Globe at Night project.

This page recaps what we discussed as a group.


Introduction

It's British Science Week! So we're going to do some scientific computing in this workshop.

We will writing code using the Python programming language - this is often used by scientist to do their research!

We will use Trinket as our editor. You can run and edit the code snippets directly from these instructions, or open each file seperately.

Save your work by saving the link from the sharing menu, or by remixing the file into your own Trinket account.

Screenshot of the Trinket editor

Be patient when you code in Python - you will get error messages as you go along and this is completely normal! Whenever you make changes to your code, run the new program and make sure it works - we will discuss troubleshooting tips as we go along.



Beginning the Quiz

Run this code buy clicking the > button on the top.

You can access this directly on the Trinket website by clicking here.

Can you edit the code to show the answers instead of the questions?


So how does this work?
Screenshot of code from editor above, described below
  1. We tell the program to open the text file with the quiz inside.
  2. It looks at each line within that file, one at a time. This line of code is called a for loop.
  3. On the line, when it finds a dash - symbol, it splits the line in half - this is how we seperate the questions from the answers!
  4. It labels the first part question, and the second part answer, using variables.
    Note that Python starts counting from 0 instead of from 1, so line[0] is the first half of the line!
  5. It removes extra spaces from the end of each part.
  6. We tell it to print all the questions - this is why we see them in the Results part of the editor.
  7. Finally, because it doesn't need to look at the quiz any more, we tell it to close it.

Did you notice the gap on the left before some of the lines? This is called indentation, and is how Python knows which lines of code are inside the for loop!


Can you add some of your own questions to the quiz?
Check that the code still works if you print all the questions, and if you print all the answers!

Inputting Answers

Instead of putting all the questions on the screen at once, we can tell the program to print the first one, then wait for user input.

Click on line 10, after print(question), and add the following code:

response = input("What is the answer?")

Test this code - what happens when you type in an answer?

If you get an error message, check your indentation - make sure the new code lines up with the line before it.


We can improve the quiz game by checking if the response given is the same as the answer in the file.

Make a new line after line 10 and type the code below - make sure the new code lines up with with code above it.

if response == answer:
  print("Correct, well done!")

Test it out:

If you get an error:


You can expand this code further by getting something different to happen if the answer is not correct.

else:
  print(" ")

Try adding this to your code - where should you put it? What do you want to put in the brackets?

Remember to keep testing your code to check for errors!

To view the full code example, click here.


What next?

Click here to move on to the Globe at Night project

A few ideas of things to add to your quiz are below - or ask us for help to implement your own ideas!