#I have to revise this code below to allow the teacher to perform these calculations for any number of students. The teacher will be asked if she wants to calculate the average of another student. If she does, the program will allow her to input his / her test grades then calculate the new students average. The program should continue accepting new students and grades until the teacher says she has entered the last student's grades.
#What I have so far is almost right, but I can't seem to get it to completely work it messes up at the very end.

count=0
totalPoints=0.0
moreGrades="yes"
while moreGrades != "no":
    testPoints=input("Enter a test grade. ")
    totalPoints= totalPoints + testPoints
    moreGrades=raw_input("Are there any more grades to enter? ")
    count= count + 1
    average= totalPoints/count
print "The average is", average
print
moreStudents="yes"
moreStudents=raw_input("Would you like to calculate another students average? ")
while moreStudents != "no":
    testPoints=input("Enter a test grade. ")
    totalPoints= totalPoints + testPoints
    moreGrades=raw_input("Are there any more grades to enter? ")
    count= count + 1
    average= totalPoints/count
print "The average is", average
    

    
     
        
   
raw_input()

Recommended Answers

All 4 Replies

What exactly is happening incorrectly in this program?

I would offer some advice to clean it up a little... I would create a menu for this.

(

i = 0
while i == 0:
    print "Press 1 for Selection 1"
    print "Press 2 for Selection 2"
    print "Press 3... End Program... Etc."
    
    menu = input("Choose Your Selection")
    if menu == 1:
        print "Blah Blah"
        print "do your function"

    elif menu == 2:
        print "second selection do whatever"

    else:
        print "Thanks for using whatever...:"
        i = 1

hope that helps if not post back with specifics... i'd be happy to help

Note: Zemeus is using Python 3, but you appear to be using Python 2.x.

I would avoid the menu myself, for this exercise, instead I'd use two nested loops

more_students = True
while more_students:
  more_score = True:
  grade_total_for_student = 0.0
  test_count_for_student = 0
  while more_score:
    score = int(raw_input('Please enter the next score for this student. Enter -1 to end: '))
    if -1 == score:
      break
    # We didn't break. Do some stuff with the student total and count
  # we got here by breaking from more_score
  print ("Result for this student = %s"%(grade_total_for_student/test_count_for_student))
  do_more = raw_input("Press 'Y' to do another student, 'Q' to quit: ")
  if q += do_more.lower():
    print ("Thanks for using my program!")
    break

Line 7 assumes test grades are integers. If not, you need to fix it.
Line 12 is deliberately not quite right. You will want to do your own calculation, whatever it is, and probably do some formatting too.

If you want to allow the teacher to enter a "Quit" or some such command, then you can use a try: ... catch: block around line 7: If the cast fails, it will throw a ValueError and you can break out of the loop in the catch block.

griswolf put few bugs there for you to debug. Hint how quotes are used in Python?

I think this is the answer she is looking for

moreStudents = "yes"
while moreStudents !="no":
    count = 0
    totalPoints = 0
    moreGrades = "yes"
    while moreGrades !="no":
        testPoints = input("Enter a test grade ")
        totalPoints = totalPoints + testPoints
        moreGrades = raw_input("Are there any more test grades to enter? ")
        count = count + 1
        average = totalPoints / count
    print "The average is ", average
    moreStudents = raw_input("Are there any more students? ")
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.