Step 3 – Asking a question and getting an answer
What else did you add to your welcome?
The next thing to do is to ask the first question.
IMPORTANT: Make the first question a multiple choice question. This is easier to code for now.
You can always change the order of questions around later on.
To do that we need to use print alongside a new command: input.
Asking a question
So, let's ask the first question.
Add this underneath the code you already have
Type this print code first: print("Question 1: what is the capital of France?")- Run this now. You can see that it just prints like before
- Now we need to give the possible answers. Do these one on each line – use the screenshot to give you the code
- Run it again. Make sure it looks OK and makes sense
Getting an answer
Now we need to ask the user for an answer.
THEORY: this is going to involve using a variable to store the user's answer. I'm going to call my variable answer because that makes sense to me.
Remember that a variable is just a named area of computer memory where we can store something.
Now, underneath type: answer = input("Your answer > ")
IMPORTANT: actually type the words "Your answer". This gives the user a prompt to type their answer in. Don't type the actual answer to your question!
Don't forget the quotes (") and brackets. And do you see that I added a space after the symbol?
- Now run the program again. This time it waits for you to enter an answer
- Type an answer and press Enter. The program will just end at that point for now. That's OK
The > symbol is a classic way to show a player that they need to enter something.
THEORY: an input command lets you get the user to enter something. This could be a word, a number, a sentence or just getting them to press enter. You can store whatever the type in a variable and then use that data later on.
You can see my finished code by clicking the button below.
I left some gaps between my code. This just makes it easier to read. Which is a good thing.