[Tutor] Tutor Digest, Vol 55, Issue 84

Lie Ryan lie.1296 at gmail.com
Mon Sep 29 12:14:58 CEST 2008


On Mon, 2008-09-29 at 03:00 +0200, tutor-request at python.org wrote:
> 
> Message: 2
> Date: Sun, 28 Sep 2008 23:36:07 +0000
> From: tsmundahl at comcast.net
> Subject: [Tutor] Hello and some questions.
> To: tutor at python.org
> Message-ID:
>         <092820082336.1795.48E014E70004ADE800000703221652796604080E0B029A039C9B at comcast.net>
>         
> 
> Hello all. 
> 
> I recently starting taking Python in my state college. I joined the
> Python.org users group and I posted a couple of codes examples that I
> was having problems with, and I am wondering if there is anyone in
> this community that can help me. I did get some great advice on my
> previous posts however my code is still not working. I have two
> problems that I have to write code for. 
> 
> The first problem is a program that will take a list of ten grades
> from a user and then calculate the average of the ten numbers entered.
> The user can only use the numbers 1-10. Example. "The total average of
> the grades you entered in was 5" which would be the average using
> number 1-10 in a sequence, but it needs to calculate an average no
> matter if the user puts in 3 "5's" and 7 "10's". The average must be
> calculated. 
> 
> I did get some help in the python forums with this and I have come up
> the following, but I am getting a syntax error on the line that calls
> out "total" as a variable. Here is the code:
> 
> value = [ ]
> 
> for i in range(10):
>     range += 1
>     print (int(float(raw_input("Please enter a grade, use numbers 1 -
> 10: ")))       
>      
> # getting a syntax error on this line    
> total = sum(value)
> 
> average = total/10.0    
>        
> print "The average sum of all of the grades that you entered is ",
> ("%.2f" %  average)
> 
> 
> raw_input("\nPlease hit enter to quit.")
> 
> ///The code does not work. 

This is what you want:

# make integer division returns float
from __future__ import division

# set grade to empty list
grades = []

# do something 10 times
for i in range(10):
    # prompt user for grade and convert it into int
    grade = int(raw_input('Please enter a grade: '))
    # append the grade to the list
    grades.append(grade)

# sum() returns the sum of the list
# len() returns the number of element in the list
average = sum(grades) / len(grades)

# print the result
print 'Average: %.2f' % average

> The next problem is supposed to use the while loop. A user is supposed
> to enter some grade values. The user will continue to be prompted to
> enter grade values until the user has entered in "9999" as  a grade
> value. Then the program is supposed to calculate the average of all of
> the grades entered including the 9999. 
> 
> Here is my code, This code is not working either:
> 
> target = 9999
> 
> value = [ ]
> 
> while i in value < or not == target:
>       i += 1
>       print grade (int(float(raw_input("Please enter a grade: ")))
>      
>                   
> total = sum(value)
> 
> average = total/target
> 
> print "The average grade that you entered is ", ("%.2f" % average)
>         
> raw_input("Please hit enter to exit.")
> 
> 
> ///Can anyone tell me what I am doing wrong on either of these
> problems? I know it is a logic error, but I am new to this so please
> bare with me. Again, thanks for your help. I am new to this forum so
> please have patience with me when I am posting. 
> 
> Thank you, 
> 
> 
> 
> Thomas Mundahl



More information about the Tutor mailing list