I am trying to write a program that allows you to enter the number of students in a class, and then enter 3 test grades for each student to calculate averages. I am new to programing and I keep getting an error that I don’t understand what it means or how to fix it. This is what I have so far:
students=int(input('Please enter the number of students in the class: '))
for number in students:
first_grade=(input("Enter student's first grade: "))
second_grade=(input("Enter student's second grade: "))
third_grade=(input("Enter student's third grade: "))
When you wrote
your intention was, “run this block of code
studentstimes, wherestudentsis the value I just entered.” But in Python, the thing you pass to aforstatement needs to be some kind of iterable object. In this case, what you want is just arangestatement. This will generate a list of numbers, and iterating through these will allow yourforloop to execute the right number of times:Under the hood, the
rangejust generates a list of sequential numbers:In your case, it doesn’t really matter what the numbers are; the following two
forstatements would do the same thing:But using the
rangeversion is considered more idiomatic and is more convenient if you need to alter some kind of list in your loop (which is probably what you’re going to need to do later).