N5 – Arrays (revision)

To read and write information to an array in Python we must first create the arrays, lines 4&5 do this for us.

The for loops on lines 8,12 & 17 are used to process each of the arrays in turn

  • The for block for line 8 and 9 gets the 10 subjects and stores them in the subjects array.
  • The for block on line 12 and 13 displays each subject name and stores the marks for that subject.
  • Finally lines 16-20 display the information from each array in one table.
# get and display 10 subjects and marks

# create two arrays to store the information
subjects=[""]*10
marks =[0]*10

#Get all the subjects
for index in range(10):
    subjects[index]=input("What is the name of subject" + str(index+1))

#Get all the marks
for index in range(10):
    marks[index]=int(input("What is the mark for " + subjects[index]))

#display subjects and marks
print("-"*25) # Pretty divider
for index in range(10):
    print(subjects[index],"\t",marks[index])

print("-"*25)    


Leave a Reply

Your email address will not be published. Required fields are marked *