0

I'm creating a program that asks for the number of students, then asks for their names.

Ex:

Enter the test scores of the students:
> 4

When I use the same method for grades, it won't work (the final output desired is the name of the student next to the grade). My second loop doesn't seem to work.

Desired Output:

Enter the test scores of the students: 5
Bob
Tom
Obi
Eli
Brady (only lets me add 5 names)
Enter the test scores of the students:
100
99
78
90
87 (only lets me add 5 grades)
OUTPUT:
Bob 100
Tom 99
Obi 78
Eli 90
Brady 87

Here is the code I have tried:

students = []
scores = []
count = 0
count2 = 0
number_of_students = int(input("Enetr the number of students: "))
while count != number_of_students:
                           new_student = input()
                           students.append(new_student)
                           count = count + 1
                           if count == number_of_students:
                               break

print("Enter the test scores of the students: ")
while count2 != count:
    new_score = input()
    scores.append(new_score)
    count2 = count2 + 1
    if count == number_of_students:
        break

What can I change?

1 Answer 1

1

I think this is a case where you're making the problem harder than it is. You don't need to check the end condition both at the while and at the end of the loop -- just once should be fine. Also you don't need a counter for the second loop, you can just loop over the names from the first loop:

students = []
scores = []
count = 0

number_of_students = int(input("Enter the number of students: "))

while count < number_of_students:
    new_student = input("Student name: ")
    students.append(new_student)
    count = count + 1

print("Enter the test scores of the students:")

for name in students:
    new_score = input("Score for " + name + ": ")
    scores.append(new_score)

But whenever I see parallel arrays like this, an alarm goes off that says you need a better data structure. Perhaps an array of tuples or a dictionary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.