Answer:
Hey there! I remember based off the last one we needed a beginner friendly version rather than a advanced version! So with that in mind...
names = [None] * 5
ages = [0] * 5
# Collect the names and ages of the 5 students
for i in range(5):
name = input("Please enter the name of student " + str(i + 1) + ": ")
age = int(input("Please enter the age of student " + str(i + 1) + ": "))
names[i] = name
ages[i] = age
# Calculate the average age
average_age = sum(ages) / len(ages)
# Find the oldest student
oldest_age = max(ages)
oldest_index = ages.index(oldest_age)
oldest_name = names[oldest_index]
# Print the results
print("The average age is:", average_age)
print("The oldest student is " + oldest_name + " with an age of " + str(oldest_age))
Step-by-step explanation:
We create two lists, names and ages, with 5 elements each. The names list is initialized with None values, and the ages list is initialized with 0 values.
Then, we use a for loop to iterate 5 times, as we want to collect information about 5 students. In each iteration, we ask the user to input the name and age of a student.
We use the input() function to get the name of the student and directly assign it to the corresponding index in the names list using names[i] = name.
We use the int() function to convert the user input into an integer (since age is a whole number) and directly assign it to the corresponding index in the ages list using ages[i] = age.
After collecting all the information, we calculate the average age by summing up all the ages in the ages list using the sum() function and dividing it by the number of students (in this case, 5).
To find the oldest student, we first determine the highest age using the max() function, which returns the maximum value in the ages list. Then, we use the index() method to find the index of the oldest age in the ages list. With this index, we can find the corresponding name of the oldest student in the names list.
Finally, we print the average age and the name and age of the oldest student using the print() function.
NOTE: The main difference in this version of the code is that we initialize the lists with a fixed size and assign values directly to each index instead of using the append() method to add elements to the lists.