1.1 - Display linked data
Write a program that asks the user to enter the name and grade for three pupils. Store the names in one array and the grades in another (parallel arrays). After all details have been entered, display each pupil’s name followed by the grade they achieved as shown.
Enter pupil 1 name: Jane Enter pupil 1 grade: A Enter pupil 2 name: John Enter pupil 2 grade: B Enter pupil 3 name: Jess Enter pupil 3 grade: C Test Results Jane achieved grade A John achieved grade B Jess achieved grade C
DECLARE names[3] AS STRING
DECLARE grades[3] AS STRING
FOR i FROM 0 TO 2 DO
RECEIVE names[i] FROM KEYBOARD WITH PROMPT "Enter pupil " & (i+1) & " name: "
RECEIVE grades[i] FROM KEYBOARD WITH PROMPT "Enter pupil " & (i+1) & " grade: "
OUTPUT BLANK LINE
END FOR
OUTPUT "Test Results"
FOR i FROM 0 TO 2 DO
OUTPUT names[i] & " achieved grade " & grades[i]
END FOR
names = [str] * 3
grades = [str] * 3
for i in range(3):
names[i] = input("Enter pupil " + str(i+1) + " name: ")
grades[i] = input("Enter pupil " + str(i+1) + " grade: ")
print() # blank line for spacing
print("Test Results")
for i in range(3):
print(names[i] + " achieved grade " + grades[i])
# Create two arrays of fixed size 3 to store names and grades
names = [str] * 3
grades = [str] * 3
# Loop 3 times to get input for each pupil
for i in range(3):
# Ask for pupil name at the current position
names[i] = input("Enter pupil " + str(i+1) + " name: ")
# Ask for pupil grade at the current position
grades[i] = input("Enter pupil " + str(i+1) + " grade: ")
# Print a blank line for spacing
print()
# Display results after all input is collected
print("Test Results")
# Loop again to display each pupil with their grade
for i in range(3):
print(names[i] + " achieved grade " + grades[i])
Extension
Add a third array (e.g. classes = [“1A”,”1B”,”1A”]) and display all three values on each line.
Test Results Jane in class 1B3 achieved grade A John in class 1S1 achieved grade B Jess in class 1A4 achieved grade C