Now that we have written our Christmas Quiz in Excel using ‘nested IF statements’ let’s see if we can transfer those questions into a new computer program.
We are going to write this quiz using a computer language called Python. This is a HIGH LEVEL language which means it is written in everyday English rather than very complicated ‘machine code’.
Let’s get started by going to START–> ALL PROGRAMS and finding the program PYTHON. Once there we can look for IDLE.
Now go to FILE and then NEW WINDOW.
You need to open this and then set it up ready to use.
Go to OPTIONS and then CONFIGURE. Now change the number 10 on the left hand side to 18 or 20, so you will be able to see your work more clearly.
DO NOT change anything else.
(You can download Python for FREE from http://www.python.org)
Create a welcome message
In Python the word print simply means ‘show on the screen’ it does not mean send to the printer.
Type this into your program:
print(“Welcome to the Christmas Quiz”) #this is a comment it does not affect the way the program runs
notice the following things
a) print has a small letter p at the beginning
b) the phrase you want to display is inside brackets and speech marks.
Now press the F5 key to RUN or EXECUTE your program. You will be asked to save your work again first.
Ask the user their name
Many games and quizzes personalise the messages the user sees by including their name. Obviously to use their name to have to ASK them what it is first.
Type the following:
name=input(“Please enter your username. “)
Often it makes more sense if you read these statements BACKWARDS – ask the question – expect the user to INPUT a reply – save their answer as a VARIABLE, in this case called ‘name’.
If you add the next line you can check that they have entered their name
print(name)
This time name is in brackets but there are no speech marks. This is because we are referring back to the VARIABLE they have entered.
Now let’s look at the quiz code
score=0
name=input(“Please enter your username’)
print(“Welcome to the Christmas Quiz ” +name)
answer1=input(“Question 1 – what day is Christmas Day? Is it a) 25th Dec b) 26th Dec c) 1st Jan”)
if answer1==”a”:
print(“Well done!”) #this should be INDENTED by 4 spaces
score=score+1 #this should be INDENTED by 4 spaces
print(“Your score is ” +str(score)) #this should be INDENTED by 4 spaces
else:
print(“Sorry that’s not right.”) #this should be INDENTED by 4 spaces
Once you have typed this in try running your program. Do you remember how to do that? (Go back up the page if you have forgotten)