Almost all projects using randomised comments or sentence questions with worded or numerical answers should use lists to store questions and answers
Lists are like variables but store multiple values instead of just one
Lists are written using the following syntax:
listName = ["item 1", "item 2", "item 3"]
e.g. highScores = [100, 75, 60]
e.g. questions = [
"The largest mammal is the...",
"Roots in plants undergo photosynthesis (T or F)",
"1000 cm cubed is equivalent to how many litres?"
]
e.g. answers = [
"blue whale",
"F",
"1"
]
e.g. highScores = ["blue whale", "F", "1"]
I have compiled a large number of lists for you to use
and modify for use in your projects on this page of the website
In the example to the left, there are four elements in the list / array.
We reference them from the number 0, which is the index of the first element of the list / array.
If I wanted to print, for example, the third element of the array, I would write
print(names[2])
which would print the name "Jessica".
If I wanted to print, for example, a random element from the array, I would write
print(names[randint(0,3)])
which would print one of the four names in the list.
If I wanted to remember a particular or random value from the list I would assign the content of the list index to a variable like this
randomName = names[randint(0,3)]
Examples of list usage can be found below
IMPORTANT NOTES:
If you are using worded questions and answers from lists, you MUST have ONE variable that remembers both the question index and the answer index.
Example #2 calls this variable 'qAndA'.
Example #3 calls this variable 'questionAndAnswerNumber'.
You MUST include this variable in your game planning & flowchart.
Example #1
from random import randint
goodComment = ["Excellent", "Well done", "Awesome"]
badComment = ["Bad luck", "Try again", "Terrible"]
if userAnswer == realAnswer:
print(goodComment[randint(0,2)])
else:
print(badComment[randint(0,2)])
Example #2
from random import randint
#NOTE: lists start their position counting from position ZERO
#reference list values from 0 e.g. fractionList[0] is "3/4 or 2/3"
#and fractionList[1] = "1/4 or 1/2" etc
#define lists and score variable
fractionList = ["3/4 or 2/3", "1/4 or 1/2", "9/10 or 8/9 or 19/20"]
fractionAnswer = ["3/4", "1/2", "19/20"]
score = 0
#ask random question
print("Which fraction is larger?")
qAndA = randint(0,2)
question = fractionList [qAndA]
print(question)
correctAnswer = fractionAnswer [qAndA]
userAnswer = int(input(">>> "))
if userAnswer == correctAnswer:
score +=1
Example #3
from random import randint
print("Type 'yes' or 'no' as a response to each statement.\n\n")
questionListOne = [
"It is more likely to rain on a cloudy day."
"Milk will go off when out of the fridge.",
"Your cat will not wake you up at 5am.",
"My class is well behaved." ]
answerListOne = ["yes", "yes", "no", "no"]
q = 1
currentQuestionAndAnswer = 0
while q <=5:
print("Question "+str(q)+": ")
currentQuestionAndAnswer = randint(0,3)
print(questionListOne[currentQuestionAndAnswer])
userAnswer = input("Type yes or no: ")
print("Correct answer is " + answerListOne[currentQuestionAndAnswer])
if userAnswer == answerListOne[currentQuestionAndAnswer]:
score +=1
print("Correct")
else:
print("Wrong")
q +=1