Re: [Tutor] Hello and some questions.

2008-09-28 Thread Alan Gauld

<[EMAIL PROTECTED]> 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 "" as  a grade value.


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


Really? How bizarre!


Here is my code, This code is not working either:

target = 

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  even if there are only 2 entries...
 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 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hello and some questions.

2008-09-28 Thread Andre Engels
On Mon, Sep 29, 2008 at 1:36 AM,  <[EMAIL PROTECTED]> wrote:

> 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:

The actual problem is in the previous line:

print (int(float(raw_input("Please enter a grade, use numbers 1 - 10: ")))

If you count, there are 4 (s and 3 )s in there. Because every ( should
have a corresponding ) and vice versa, Python thinks that this
statement has not finished yet. It gives a syntax error at the _next_
line, because there it finds something that cannot be a correct
continuation of this statement.

However, even if you would do that, the result would still not be what
you want. It will give 0 each time, because value will always be the
empty list. Rather than printing out the grade that the user entered,
you would want to put it in the list value (an unlucky choice of a
name for a list of grades, in my opinion)

> 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 "" as  a grade value. Then the 
> program is supposed to calculate the average of all of the grades entered 
> including the .
>
> Here is my code, This code is not working either:
>
> target = 
>
> value = [ ]
>
> while i in value < or not == target:

That line will indeed cause Python to stumble. I'm not even sure what
you want to do here, so I can't really say what it should have been.

-- 
André Engels, [EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor