0

I have to make this program:

Write a program that allows a teacher to input how many students are in his/ her class, then allow them to enter in a name and mark for each student in the class using a for loop. Please note you do not need to record all of the names for later use, this is beyond the scope of the course * so just ask them each name, have it save the names over top of each other in ONE name variable.

i.e.)

INSIDE OF A LOOP

name = input (“Please enter student name: “)

Calculate the average mark for the entire class – this will require you to use totaling. Output the class average at the end of the program, and ask if they would like to enter marks for another class. If they say yes, re-loop the program, if no, stop the program there.

So I started writing the program and it looks like this

studentamount = int(input("Please enter how many students are in your class: "))

for count in range():

    name = input ("Please enter student name: ")
    mark = int(input("Please enter the student's mark"))

I ran into the following problem: how would I allow the set of code under the for loop to loop for each student? I was thinking I could just enter in the studentamount variable as the range but I can't since python does not allow you to enter in a variable as a range.

How would I get the for loop to loop for the amount of students typed in? e.g. if 20 students for student amount was typed in, I would want the for loop to loop 20 times. Your help and knowledge is much appreciated.

  • 2
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Nir Alfasi Nov 12 '17 at 18:25
  • 1
    accept `studentamount` as user input, similar to what you did for `mark` and use it as `range` for the loop – Van Peer Nov 12 '17 at 18:26

3 Answers3

0

Read the user input, convert it to int and pass it as a parameter to range:

studentamount = input("Please enter how many students ...: ")  # this is a str
studentamount = int(studentamount)  # cast to int ...

for count in range(studentamount):  # ... because that's what range expects
    # ...
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

Python does not allow you to enter in a variable as a range.

Python does allow you to enter variables as a range, but they must be numbers. Input () reads input in as string, so you need to cast it.

So, this is correct:

```Python studentamount = int(input("Please enter how many students are in your class: "))

for count in range(studentamount):

name = input ("Please enter student name: ")
mark = int(input("Please enter the student's mark)

```

P.S a try - except clause would be useful here to catch people entering a non-integer data in [TypeError]

P.S.P.S @schwobaseggl 's example is good too, it is possibly more pythonistic to use the nested function studentamount = int(input("Text") than studentamount = input("Text") studentamount = int(studentamount)

John
  • 598
  • 2
  • 7
  • 22
0

You can store each student name and mark in a dictionary or tuple and store each dictionary (or tuple) into a list, see code sample (at the end enter "no" to exit or any other value to re-loop the program):

response = None
while response != 'no':

        student_count = int(input('Please enter the number of students: '))
        students = []
        mark_sum = 0
        print('There are {} student(s).'.format(student_count))
        for student_index in range(student_count):
            student_order = student_index + 1
            student_name = input('Please enter the name of student number {}: '.format(student_order))
            student_mark = float(input('Please enter the mark of student number {}: '.format(student_order)))
            students.append({'name': student_name, 'mark': student_mark})
            mark_sum += student_mark

        print('The average mark for {} student(s) is: {}'.format(student_count, mark_sum / student_count))

        response = input('Do you want to enter marks for another class [yes][no]: ')
Nordine
  • 1
  • 1