# Class average of each student's grade average (using a while loop)

Page 1 of 1

## 12 Replies - 12736 Views - Last Post: 16 October 2012 - 02:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295791&amp;s=fb7aca53af20312b751c57bfdd0bbd92&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

# Class average of each student's grade average (using a while loop)

Posted 15 October 2012 - 11:50 PM

Hello everyone! So I need to write a program that averages the midterm and final of a student, then averages together all those averages.

Example:

Student 1
Midterm: 100
Final: 95
Average: 97.5

Student 2
Midterm: 100
Final: 0
Average: 50.0

...and it keeps on asking for student 3, student 4, student 5, etc, until the user enters "done".

After the user enters "done", it finds the average of the average of each student.

So, this where I'm going with it:

while True:
a1 = input ('Please enter name:')

if a1 = str("done"):
????

b1 = float(input ('Please enter midterm:'))
c1 = float(input ('Please enter final:'))
average = (b1+c1)/2

print ('Name:',a1, 'Midterm:',b1,'Final:', c1,'Average:',average)

I put the ????'s there because I'm not exactly sure where to go at that point. I'm thinking that I want the program to somehow store the average of each student, then after the user enters done, it averages each stored value. I just don't know how I would go about doing that in a while loop. Can someone help?

(Also, I'm doing this for a class so I still have limited knowledge of the Python language. It would be best if all help could stick to the basic way I'm trying to do it. Anything else I haven't learned yet, and I will just get more confused. Thanks!)

Is This A Good Question/Topic? 0

## Replies To: Class average of each student's grade average (using a while loop)

### #2 darek9576

• D.I.C Lover

Reputation: 199
• Posts: 1,696
• Joined: 13-March 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 12:53 AM

I would do it this way:

Spoiler

### #3 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:01 AM

Thanks! This is definitely helpful, but the only problem is that I haven't quite learned anything outside of the while loop, so I'm having a hard time understanding exactly what it is the code is doing.

I know that it's harder to do it with only a while loop, but I'm thinking that maybe I can just have the average of each student add onto the last, then when the user enters "done". It divides that total by x amount of students. Is there a way I can do it that way? Seriously in my class I haven't even learned what def does yet.

Sorry for making it harder than it needs to be.

### #4 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• Joined: 28-December 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:11 AM

The part where you put in the while condition you should set a boolean variable just outside it and set it to true on the ???? part when you're done with your code.

### #5 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:17 AM

fromTheSprawl, on 16 October 2012 - 01:11 AM, said:

The part where you put in the while condition you should set a boolean variable just outside it and set it to true on the ???? part when you're done with your code.

I'm sorry, I don't quite understand what you mean. I'm still incredibly basic with this stuff. I'm struggling to figure it out.

### #6 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:25 AM

Thanks for all your help guys, I actually think I'm getting somewhere with this. I just need to know how to do one thing.

After "done" is entered, I need the program to add the sum of all the averages. The sum will be "totalaverages". Then i'm just going to write totalaverages/x. (X being the number of times the while loop ran.) That should give me the average of all the student's scores.

Is there a simple way to add each average together within the loop?

### #7 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• Joined: 28-December 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:26 AM

Here's a basic example dude:
def tickingBomb():
ticker = 10
timesOut = False
while timesOut == False:
ticker = ticker - 1
print ticker
if ticker == 0:
timesOut = True
print "BOOM!"

Output:

Quote

>>> getOutWhenBombExplodes()
9
8
7
6
5
4
3
2
1
0
BOOM!

What we did there is declare a counter first named ticker set to 10, then we have a boolean variable named timesOut which when evaluates to true escapes the while loop. Notice that our printing of the explosion is outside the while loop? That means we got out of the while loop because our while condition where timesOut is false is not true anymore, since we set timesOut to true when our counter reached zero inside the while loop.

### #8 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:33 AM

Ah okay! That makes complete sense. So what is the benefit of applying this to my code? Because it breaks me out of the while loop?

### #9 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• Joined: 28-December 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:36 AM

Regarding your latest post, just declare a variable inside the while loop that will be the sum of all averages of the students then when you get outside the while loop, do your division and printing. Simple example:
ef pickApples():
endOfDay = False
totalApples = 0
while endOfDay == False:
print "How many apples did you pick?"
apples = int(raw_input())
print "Is it time to go home?Yes or No"
if raw_input() == "Yes":
endOfDay = True
totalApples = totalApples + apples
print "We got a total of ",totalApples," apples. Awesome!"

Output:

Quote

>>> pickApples()
How many apples did you pick?
2
Is it time to go home?Yes or No
No
How many apples did you pick?
3
Is it time to go home?Yes or No
No
How many apples did you pick?
4
Is it time to go home?Yes or No
Yes
We got a total of 9 apples. Awesome!

Yes, regarding your post above, it breaks out of the while loop.

### #10 chasebennett

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 12-October 12

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:45 AM

Okay! I think I figured it out! I ran the code, and it seemed to work for me. This is my final result:

x=0
totalaverage=0
while True:
a1 = input ('Please enter name:')

if a1 == str("done"):
print ('The Class Average',totalaverage/x)
break

b1 = float(input ('Please enter midterm:'))
c1 = float(input ('Please enter final:'))
average = (b1+c1)/2
totalaverage = totalaverage + average

print ('Name:',a1, 'Midterm:',b1,'Final:', c1,'Average:',average)
x = x+1

fromTheSprawl, I completely understand what you mean, though. It's definitely a much better and much cleaner way to do it. I think I'm going to go back and do what you suggested to this code. I really appreciate the help.

### #11 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• Joined: 28-December 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 01:53 AM

Well if it works with what you did then you are correct then. Glad I could help.

### #12 darek9576

• D.I.C Lover

Reputation: 199
• Posts: 1,696
• Joined: 13-March 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 02:12 AM

He is using Python 3 so raw_input() won't work.

### #13 fromTheSprawl

• Monomania

Reputation: 513
• Posts: 2,063
• Joined: 28-December 10

## Re: Class average of each student's grade average (using a while loop)

Posted 16 October 2012 - 02:17 AM

Those are just examples to illustrate the while concept and taking a total while in the while loop concept.