17

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: "))
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
tinydancer9454
  • 403
  • 2
  • 11
  • 20

4 Answers4

39

When you wrote

for number in students:

your intention was, “run this block of code students times, where students is the value I just entered.” But in Python, the thing you pass to a for statement needs to be some kind of iterable object. In this case, what you want is just a range statement. This will generate a list of numbers, and iterating through these will allow your for loop to execute the right number of times:

for number in range(students):
    # do stuff

Under the hood, the range just generates a list of sequential numbers:

>>> range(5)
[0, 1, 2, 3, 4]

In your case, it doesn't really matter what the numbers are; the following two for statements would do the same thing:

for number in range(5):

for number in [1, 3, 97, 4, -32768]:

But using the range version 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).

bdesham
  • 15,430
  • 13
  • 79
  • 123
  • This the easiest way if you just want to execute the loop _students_ number of times, as you are. If you want actually to use _number_ within your loop, the line of code in arcx's answer is the correct way to go. – RobH Feb 18 '13 at 17:04
  • Sure, although be careful. If you just want to *display* the numbers, then arkx's answer will give you numbers that make sense to humans: "Student #1 is Sally Smith; Student #2 is Jimmy Jones", etc. But if you're storing values in a Python list--the indices of which start with zero--it's probably better to use the `range(number_of_students)` form and just add one to the iteration variable whenever you need to display it to the user. – bdesham Feb 18 '13 at 17:08
  • See the last sentence of my oringinal comment on this answer; also, see my comment on arkx's answer. – RobH Feb 18 '13 at 17:35
  • Thank you that makes sense. I'm still really confused on how to make this all work though. I can get it to calculate the average for each student's 3 tests, but then I need to calculate the class average and display the highest and lowest average. But I'm not sure how to do this because I don't know how to get a different name for each student's average so that it can be calculated at the end. – tinydancer9454 Feb 19 '13 at 02:28
  • for number in range(students): first_grade=int(input("Enter student's first grade: ")) second_grade=int(input("Enter student's second grade: ")) third_grade=int(input("Enter student's third grade: ")) StudentAverage=(first_grade + second_grade + third_grade)/3 print("The student's average is", StudentAverage) – tinydancer9454 Feb 19 '13 at 02:28
  • @ChelseaOtis Take a look at https://gist.github.com/bdesham/4983084. But if you have any further questions, you should open a new question instead of continuing this discussion. – bdesham Feb 19 '13 at 04:22
3

Numbers can't be iterated over. What you're probably looking for is the range function, which will create a sequence of numbers up to the number you want:

for number in range(1, students + 1):

The reason I added + 1 there is because the second argument to range is exclusive.

eagleflo
  • 1,184
  • 9
  • 14
  • The domain is number of students. It's pretty weird to start counting people from 0. I'm thinking ahead to the case where you want to do something with the number, like save it into a file or something similar. – eagleflo Feb 18 '13 at 17:02
  • This is the correct way if you are going to actually use _number_ within your loop. If you just want to execute the loop _students_ number of times, see bdesham's answer. – RobH Feb 18 '13 at 17:02
  • 1
    I would take care of the that shift when you print the number, not in the loop. If you later want to extend this code to index in to an array, you would have to remember to subtract the spurious 1. You are just inviting all manor of off by one bugs by trying to fight the natural indexing. – tacaswell Feb 18 '13 at 17:15
  • I'd rather `enumerate` a collection of students in a case like that. But fair point. – eagleflo Feb 18 '13 at 17:47
0

try this...it will work...

i=0
x = "abcd"


print("Using for loop printing string characters")
for i in range(len(x)):
    print(x[i])
Adiraamruta
  • 131
  • 1
  • 2
0

Try this one:

  students=int(input('Please enter the number of students in the class: '))

  for number in range(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: "))
biddut
  • 353
  • 3
  • 6