Input Values into Arrays

The values for an array might have to be typed in when the program is running:

01
02
03
04
05
06
07
08
09
10
#Create empty arrays of correct type and size
names = [""] * 20
scores = [0] * 20
 
#Count through the array indexes
for index in range(20):
    #ask for and save value in the array
    names[index] = input("Enter name:"))
    scores[index] = int(input("Enter score:"))
#

Input Validation

This uses the same standard algorithm for input validation of variable.

However, each reference to the variable becomes a reference to the array and its index value (lines 7 and 8).

01
02
03
04
05
06
07
08
09
10
11
12
#
for index in range(20):
    names[index] = input("Enter name:"))
 
    # get valid score
    while True:
        scores[index] = int(input("Enter score:"))
        if scores[index] < 0:
            print('Must be at least 1')
        else:
            break
#