[Tutor] Hello and some questions.

Alan Gauld alan.gauld at btinternet.com
Mon Sep 29 02:43:05 CEST 2008


<tsmundahl at comcast.net> wrote

> The first problem is a program that will take a list of ten grades
> from a user and then calculate the average
>
> value = [ ]
>
> for i in range(10):
>    range += 1
>    print (int(float(raw_input("Please enter a grade, use numbers 1 - 
> 10: ")))

This is very confused.
First the range += 1 is going to be wierd because range is a
function and you are trying to add one to it! Don't use variable
names that are the same as built in functions.

Secondly you only need to convert the value to an int. You don't
need the float conversion as well.

Thirdly, as already pointed out you never assign the value anywhere
you just print it. You want to append it to your value list. (Also as 
a
general style point its a good idea to name collections as plurals
so use "values" instead of "value" - it just reads better)

And finally the source of the error, the mismatched parens.
Remember Python errors, especially syntax errorts, are often to be
found a line or two before where they are reported.


> The next problem is supposed to use the while loop.
> A user is supposed to enter some grade values...
> continue to be prompted to enter grade values until
> the user has entered in "9999" as  a grade value.

> calculate the average of all of the grades entered including the 
> 9999.

Really? How bizarre!

> Here is my code, This code is not working either:
>
> target = 9999
>
> value = [ ]
>
> while i in value < or not == target:

I have no idea what you intended here.
It doesn't read logically as a piece of Python
or math or even English...

If you remove the < sign it might make some sense as English,
although not as Python...

while i in value or not == target might be translated into Python as

while i in value or not i == target

or

while i in value or i != target

But in both cases its wrong since you are testing whether i
is in the list of values which is not part of the excercise.

It should just be

while i != target:

where wec assume i is the value input by the user.

>      i += 1

But you don't want to increment i you want to get it from
the user, so

i = int(raw_input(....))

Same mistake as before: you are not storing the values
entered anywhere.

>      print grade (int(float(raw_input("Please enter a grade: ")))

And you haven't assigned anything to grade and you are simply
printing the input value rather than storing it in values

> total = sum(value)
> average = total/target

And this divides the total by 9999 even if there are only 2 entries...
9999 was supposed to be the magic value used to stop the entry process
not a count of how many entries were made.

> print "The average grade that you entered is ", ("%.2f" % average)

You need to read up on string formatting too. It should be:

print "The average grade that you entered is %.2f" % average

> 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

It's more than just logic errors you have some basic syntax
issues as well as failing to understand the concepts of
variables storing values during processing. print displays
but assignment(=) stores. And append() adds to a list...

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list