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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # 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 ) |