Back

Activity 1.23: Parallel Lists

divider

Activity 1.23

Parallel Lists

Key Concepts

Parallel Lists

Two Lists, Same Positions

Parallel lists are two separate lists where the same index refers to related data in both — here, questions[i] and answers[i] always go together.

questions = [
"What planet is known as the Red Planet?",
"How many bits are in a byte?",
"What language have we been learning?",
]
answers = [
"mars",
"8",
"python",
]

Walking Both Lists Together

This only works with index-based traversal — for i in range(len(questions)): — since you need the same i for both lists.

for i in range(len(questions)):
print(questions[i])
print(answers[i])

Walking Both Lists Together

Terminal window
What planet is known as the Red Planet?
mars
How many bits are in a byte?
8
What language have we been learning?
python

Today's Project: Trivia Game

Ask each question, compare the player's answer to the matching entry in answers, and keep score — combining everything from this unit into one program.

Today's Objectives

  • Keeping two lists lined up by index
  • Comparing a player's answer to a parallel list
  • Tracking a score with the accumulator pattern

Key Terms

Parallel Lists
Two or more lists where the same index refers to related values across all of them.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-23-trivia-game.


Task 1: Set Up the Trivia Data

  • Create a questions list and a parallel answers list — keep them the same length.
Parallel Lists
questions = [
"What planet is known as the Red Planet?",
"How many bits are in a byte?",
"What language have we been learning?",
]
answers = [
"mars",
"8",
"python",
]

Task 2: Ask Each Question

  • Loop through both lists by index, comparing the player's answer to the matching entry in answers, and track a score.
Parallel Lists
questions = [
"What planet is known as the Red Planet?",
"How many bits are in a byte?",
"What language have we been learning?",
]
answers = [
"mars",
"8",
"python",
]
score = 0
for i in range(len(questions)):
response = input(questions[i] + " ").lower()
if response == answers[i]:
print("Correct!")
score = score + 1
else:
print(f"Incorrect. The correct answer was {answers[i]}.")

Task 3: Report the Final Score

  • After the loop, print the score out of the total number of questions, with a perfect-score message.
Parallel Lists
questions = [
"What planet is known as the Red Planet?",
"How many bits are in a byte?",
"What language have we been learning?",
]
answers = [
"mars",
"8",
"python",
]
score = 0
for i in range(len(questions)):
response = input(questions[i] + " ").lower()
if response == answers[i]:
print("Correct!")
score = score + 1
else:
print(f"Incorrect. The correct answer was {answers[i]}.")
print(f"You scored {score} out of {len(questions)}.")
if score == len(questions):
print("Perfect score!")
else:
print("Keep practicing!")
divider

Checkpoint

Verify your program works correctly.

Example Output
What planet is known as the Red Planet? mars [Enter]
Correct!
How many bits are in a byte? 8 [Enter]
Correct!
What language have we been learning? javascript [Enter]
Incorrect. The correct answer was python.
You scored 2 out of 3.
Keep practicing!
divider

Reflection

Answer the following questions before submitting your work.

  1. Why must questions and answers stay the same length for this program to work?
  2. Why does this activity use for i in range(len(questions)): instead of for question in questions:?
  3. Which earlier activity's accumulator pattern did the score tracker reuse?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete