Member Avatar for Member #1005004

I am having problem with my python homework. My homework is asking to create a Python program to calculate a student test averages regardless of how many tests will be averaged. Also, it is asking to allow the teacher to continue averaging grades for any number of students. This is what I have so far. My problem the second part is not calculating right, what I'm I missing:

`I

#First Step. (Similar example pg. 104)
    count = 0
    totalGrade = 0.0
    moreGrades = "y"
    while moreGrades != "n":
        grade = int(input("Enter a test grade: "))
        totalGrade = totalGrade + grade
        moreGrades = input("Are there amy more test grades to enter, 'y' or 'n' ")
        count = count + 1
        average = totalGrade/count

    print("The average is", round(average, 1))

    #Second Step

    plusStudents = "y"
    plusStudents = input("Are there any more students, 'y' or 'n'? ")
    while plusStudents != "n":
        grade = int(input("Enter a test grade: "))
        totalGrade = totalGrade + grade
        moreGrades = input("Are there amy more test grades to enter, 'y' or 'n' ")
        count = count + 1
        average = totalGrade/count

    print("The average is", round(average, 1))

Dani AI

Generated

Short version: use a nested loop (outer loop for "another student?", inner loop for that student's grades), reset the per-student counters before each inner loop, compute the average after the inner loop, and validate input. As pointed out, your second loop also never updates the "more students" sentinel — that stops it from ending. , resetting count and total per student will fix the wrong averages.

Example pattern (different from the code already posted):

def average_for_one_student():
    total = 0.0
    count = 0
    while True:
        s = input("Enter grade (or type done): ").strip().lower()
        if s == "done":
            break
        try:
            g = float(s)
        except ValueError:
            print("Please enter a number or 'done'.")
            continue
        if g < 0 or g > 100:
            print("Grade out of range (0-100).")
            continue
        total += g
        count += 1
    if count:
        print("Average: {:.1f}".format(total / count))
    else:
        print("No grades entered for this student.")

while True:
    average_for_one_student()
    again = input("Average another student? (y/n): ").strip().lower()
    if not again.startswith("y"):
        break

Troubleshooting notes: compute the average only after the inner loop so you don't repeatedly recalc; always reset total and count inside the outer loop (or inside a function); protect against division-by-zero when no grades were entered; sanitize responses (.strip().lower()); and prefer a clear sentinel like done or an empty line instead of repeatedly asking y/n after every grade. If your course has covered functions, wrapping the inner logic as shown eliminates duplicated code and makes the flow clearer.

Recommended Answers

All 2 Replies

The problem is that you aren't resetting either count or totalGrade before beginning the second loop. also, in both loops, you can (and probably should) move the calculation of average out of the loop body.

I think you'll also have a problem with the second loop's end condition, as you aren't changing plusStudents anywhere in the loop.

Mind you, there are ways to avoid having to have two otherwise identical loops one right after another. Has your course covered functions yet? For that matter, are you certain that these should be separate loops, as you have it here? I suspect that what you want is to nest the loops, with the outer loop being the test for whether to add more students, and the inner loop being the one to read in the test grades.

Member Avatar for Member #1005004

hmmm..okay I must be doing something wrong..it is all jacked up now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.