Python Forum
Student grade program help debug
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Student grade program help debug
#1
Hello everyone. I am writing a program for school. I hit two snags. My first problem is when I run the program, the results are only printing only the name of the first student. The other names are showing up as numbers. The second problem is that the number of tests per student should be fixed at 3, but the number of tests becomes the same number as the students inputed by the user. So 5 students then 5 grades. Any help would be appreciated, and any other revisions you think i should consider, let me know thanks.

# Write a program called StudentGrades using lists and loops to ask for student name and 3 test scores Calculate the average of the test scores and assign a letter score. Write for 3 students, then modify to ask user how many students. Create separate lists for Student, Score1, Score2, Score3.  Use "FOR" loops to populate and extract data from the lists.

def calcGrade(num):
    if num > 89:
        return 'A'
    elif num > 79 and num <= 89:
        return 'B'
    elif num > 69 and num <= 79:
        return 'C'
    elif num > 59 and num <=69:
        return 'D'
    else:
        return 'F'
        
amountOfStudents = int(input('Plese enter the amount of students: '))
i=0
studentNames = []
names = []
while i < amountOfStudents:
    studentNames.append([])
    student = input('Please enter the name of the student #' + str (i+1) + ": ")
    studentNames[i].append(student)
    names.append(student)
    i=i+1
    
j = 0
avg = []
grade = []
totalSum = 0

for name in names:
    i = 0
    totalSum = 0
    while i < amountOfStudents:
        student = float(input("enter test #"+ str(i+1) + " for " + str(name) + ": "))
        totalSum = totalSum + student
        studentNames[j].append(student)
        i = i + 1
    averageScore = totalSum / 3.0
    avg.append (averageScore)
    grade.append (calcGrade (averageScore))
    j = j + 1
    
    
i = 0

for name in studentNames:
    print(str(name[i]), "'s average test score is ", avg[i])
    print(str(name[i]), "'s average test score is ", grade[i])
    i = i + 1
    
Reply
#2
Your first problem is because the loop on line 47 is screwey. You looping through studentNames, but you are also incrementing an index i (BTW, single letter variable names make your code harder to read, you should use more descriptive names). So say you have Andy, Bob, and Craig. The first time through the loop, name is a list with 'Andy' and Andy's grades, and i is 0. So name[i] gets 'Andy'. The second time through the loop name is a list with "Bob" and Bob's grades, and i is 1. So name[i] get's Bob's first grade, not his name.

The second problem is the for llop on line 34, which is going to ammountOfStudents. It should go to 3. And really it should be a for loop (for test in range(3)). Every while loop in your code should be a for loop.

Have you worked with dictionaries yet? This would work much better with a dictionary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I have read about dictionaries, i am still not confident in using them. What part of my program would use it? What would I be pariing together? Can you give me an example please?
Reply
#4
You could use a dictionary to pair up the student's name with the average grade. Essentially, you're already doing this with your lists. Any time that you index two sequences (lists) to combine their data, you're effectively using a dictionary in a roundabout way:

student_grades = {
    "Jane Doe": 99,
    "John Doe": 87,
    "Jane Doe II, electric boogaloo": 85
}

# Returns 85.
student_grades["Jane Doe II, electric boogaloo"]
# Returns 85 or 0 if the entry doesn't exist.
student_grades.get("Jane Doe II, electric boogaloo", 0)

# Sets the value based on the key.
# Also, Jane's parents are rad for giving her a subtitle.
student_grades["Jane Doe II, electric boogaloo"] = 2525
# Returns 2525.
student_grades["Jane Doe II, electric boogaloo"]
Then, you can call calcGrade() at the end after retrieving the grade from the dictionary. This would have the added benefit of eliminating the lists "avg" and "grade". In fact, by changing that to a dictionary and changing the while loops to for loops, you can reduce the code by almost half.

The lists names and studentnames are redundant. I see that they're used for two different loops but lists can be reused. So, you could eliminate one of them and just use the other for both loops.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Method to calculate average and grade ajitnayak1987 8 6,218 Apr-28-2022, 06:26 AM
Last Post: rayansaqer
  Student project - alert action when X happens Y amt of times, how? unknown00 2 1,779 Aug-25-2021, 08:07 PM
Last Post: namarang
  Non Grade/School Help - PLEASE gbyrne12 8 3,018 Jun-19-2021, 07:31 PM
Last Post: snippsat
  Student grader and sorter for assignment RazeAD 7 3,140 Feb-11-2021, 06:29 AM
Last Post: RazeAD
Sad [split] HELP ME DEBUG - Who wants to be a millionaire Game Beetoh 1 2,040 Dec-18-2020, 06:09 PM
Last Post: buran
  Generating a student's transcript [OOP concept] aongkeko 2 2,646 Dec-01-2020, 06:43 AM
Last Post: buran
  Create code for input names and score for 15 student Davin 2 2,032 Sep-21-2020, 08:49 AM
Last Post: DeaD_EyE
  New Python Student = Does this code look right? musicjoeyoung 6 3,353 May-07-2020, 02:39 PM
Last Post: musicjoeyoung
  Calculating Grade Average IstvanCH 5 5,015 Jan-27-2019, 04:42 PM
Last Post: aakashjha001
  Grade Loop dtweaponx 8 12,317 Oct-17-2017, 02:01 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020