Coding Curriculum - Python

Worksheet 1

Alright, so it looks like you're ready to code! These are the steps we are going to follow, and by the end you will have a set of Q&A's to get your puzzle started:

  1. Ask the user what subject they would like to study.

  2. Ask the user how many questions they have.

  3. Create two empty lists: one that can store the questions, and one that can store the answers.

  4. Ask the user to enter their question and answer, as many times as they said they had questions.

  5. When the user inputs a question or answer, make sure you add it to the corresponding lists you created before.

  6. Once you have entered all the questions and answers, you are done! But there are some features you might want to add...

Step 1. Ask for the subject

The computer needs to ask you what subject the questions you are going to enter are for. Don't forget to store the answer in a string variable!

yourVariable = raw_input("Add what you want to ask here.")

Step 2. How many questions do you have?

Again, you need to ask the user for input. But remember that this time, you are asking for a number (so make sure you use the right method to not store the answer as a string of characters!)

anotherVariable = input("Ask something again.")

Step 3. Q&A list

When you type in the questions and answers, you need to save them somewhere. You can do this by creating two empty lists (one for the question, one for the answer).

yourList1 = [] # make sure your list has nothing in it, you are just initialising
yourList2 = []

Step 4. Populate the lists

Here, you need to keep asking the user to enter their questions and answers for as many times as they said they had questions!

There are different ways of using this, but here we will use a while loop (your other option is to use a for loop, give it a try!).

Pay attention to the variable index: it is an integer that is used to keep track of the position in the list. However, we did not start it at 0 because we are printing out "Enter question [insert index here]", and asking "Enter question 0" doesn't make much sense! This is fine, you just have to keep in mind that your first question is stored in the position 0.

index = 1
while index <= somethingButWhat:
variable1 = raw_input("Ask to enter something")
variable2 = raw_input("And ask to enter another thing")
index = index+1

Step 5. Save the Q&A's to their list

Once the user has entered the question and the answer, you need to save it inside the list - otherwise it will disappear when you enter new ones.

Here we are using str(index), which transforms the integer index into a string, so that you can glue it to the sentence. This is good, because it lets whoever is reading the Q/A to know which number they are reading.

index = 1
while index <= total:
variable1 = raw_input("Enter question {0}: ".format(index))
yourList1.append( str(index) + '. ' + variable1)
variable2 = raw_input("Enter the answer: ")
yourList2.append( str(index) + '. ' + variable2)
index = index+1

Why can we have separate lists you ask? How do we know what question lines up with which answer, if they aren't in the same list? Again, it's all in the index. In your while loop, you only increment (or go to the next index) when you do: index = index+1, and this is done after you enter both the question and the answer. So, the question and the answer are saved in the same index position. It looks something like this:

index questionList answerList
0 question #1 answer #1
1 question #2 answer #2
2 question #3 answer #3

Now when you want to use questionList[2], you know the answer is in answerList[2].

The Complete Code

Now, let's put all these steps together into a method that we can name setupQnA. Remember you don't have to use the same names as we did, so your code can look a little bit different:

def setupQnA():

subject = raw_input("Hello! Let's make a wordsearch. First, tell me your subject: ")
total = input("Great! How many questions do you have? ")

# Initialise the variables
questionList = []
answerList = []

index = 1
while index <= total:
question = raw_input("Enter question {0}: ".format(index))
questionList.append( str(index) + '. ' + question)
answer = raw_input("Enter the answer: ")
answerList.append( str(index) + '. ' + answer)
number = number+1

print("You entered all the Q&A's. Now let's play!")

setupQnA()
#Don't forget to call your method at the end! Or nothing will be executed

Some Extra Thinking...

  • Printing the questions and answers

Maybe you want all the questions and answers to be printed on the screen This would make it easy to proof read for example, or just to see what you typed in.

To do this you can use a for loop to iterate (or run through) the two lists, and printing the content at each position of the list. Remember we have separate lists, so you need a for loop to go through all of the questions, and one to go through all the answers.

for question in questionList: 
for answer in answerList:
print question
print answer
  • Another feature to perfect your code would be to look at some errors that can occur. What if the user types in something else than a number, when you ask how many questions they have? If that's the case, you can add an if-statement to tell the user "Error: you did not enter a number! Please try again."
total = input("Great! How many questions do you have? ")

if total.isdigit() == False:
print "Error: you did not enter a number!"
total = input("Please enter a number: ")

Congrats, you made it through the first worksheet! Good job... but more coding awaits :)