do you guys help newbies??

Robin Munn rmunn at pobox.com
Wed Nov 27 10:53:20 EST 2002


malik m <malik_martin at hotmail.com> wrote:
> hi yea i messed up here's the code sorry about the html too i was
> using it through hotmail while i learnd and still am learning how to
> use newsgroups....

That's OK, happens to all of us. This one came through just fine as
plain text.

> #chapter 3 test control structures 3.3
> print "hello, this program will tell me how many miles per gallon your
> car drives by the \n amount of miles entered by the user along with
> the size of his/her tank."
> 
> #initialize variables
> totalMilesDriven = 0 #total miles driven
> 
> totalGallonsPerTank = 0 #total gallons per tank
> 
> counter = 0 #bogus counter
> 
> #processing phase
> gallonsPerTank = raw_input("Enter how many gallons your tank holds, -1
> to end:")
> gallonsPerTank = int(gallonsPerTank)

You could also do this in one step, like:

    gallonsPerTank = int(raw_input("Enter how many gallons..."))

> 
> milesDriven = raw_input("enter how many miles driven, -1 to end:")
> milesDriven = int(milesDriven)

Here, too, you could do:

    milesDriven = int(raw_input("Enter how many miles..."))

> while gallonsPerTank or milesDriven != -1:

This line may not do what you think it does. Comparison operators have
higher priority than Boolean operators. This means that this line works
out as if it had parentheses around it like this:

    while (gallonsPerTank) or (milesDriven != -1):

What this line is doing is: first it checks to see whether
gallonsPerTank evaluates to a true value. In Python, *any* value can be
evaluated as true or false. The rules are this. For numbers, 0 is false,
any other number is true. For sequences (like lists and tuples), an
empty list, [], or an empty tuple, (), is false, and any other sequence
is true. For dictionaries, an empty dictionary, {}, is false, and any
other dictionary is true.

So: the while statement above first checks that gallonsPerTank is true
(i.e., non-zero). And if gallonsPerTank really is non-zero, then *it
never evaluates milesDriven at all*. The reason for this is because of
"shot-circuiting" Boolean operations, are really useful things once you
understand how they work. See, "(True) or (anything)" will evaluate to
True, no matter what that "anything" is. And "(False) and (anything)"
will evaluate to False, again no matter what that "anything" is. So if
the first half of an "or" operation has a true value, the second half is
never evaluated. And if the first half of an "and" operation has a false
value, the second half is never evaluated.

But all this is a bit of a tangent. What I think you really meant to do
above was check both gallonsPerTank and milesDriven, and if either one
of them was -1, then quit. Is that right? In that case, what you really
want is this line:

    while (gallonsPerTank != -1) and (milesDriven != -1):

Note the "and" instead of the "or". I figured that if *either*
gallonsPerTank *or* milesDriven was -1, you want the test to fail. That
means that both of them have to *not* be -1 for the test to succeed,
hence the "and".

>     averagea = milesDriven / gallonsPerTank

Once you've got your program working, you're going to be surprised by
the result of this line! Try it as-is, then put the following line at
the top of your program:

    from __future__ import division

And run it again. Notice the difference? I don't want to go into the
reasons for this right now, as this response is getting quite long
enough already. But the following page may help to explain what is going
on:

    http://www.python.org/doc/current/whatsnew/node7.html

If that doesn't help explain things, then go ahead and ask another
question on this newsgroup, and someone will be happy to explain it to
you.

Also, if the user ever types in 0 for gallonsPerTank, this line will
fail with a ZeroDivisionException. Since you're not catching exceptions,
that will cause your progrm to quit immediately with an error message
(and probably a traceback of the line that caused the exception).
Exception handling is a larger topic than I really want to go into here,
so I'll just point you to the appropriate page in the Python Tutorial
for now:

    http://www.python.org/doc/current/tut/node10.html

Again, if you have more questions after reading that, go ahead and ask;
people will be happy to enlighten you.

>     counter = counter + 1
>     print "your miles per gallon are", averagea
>     totalMilesDriven = totalMilesDriven + milesDriven
>     totalGallonsPerTank = totalGallonsPerTank + gallonsPerTank
>     gallonsPerTank = raw_input("Enter how many gallons your tank
> holds, -1 to end:")

Ah. Here's your problem: this time, gallonsPerTank is getting a string,
not an int. You need to convert it to an int the way you did above.

>     milesDriven = raw_input("enter how many miles driven, -1 to end:")

Ditto here. Convert this to an int as well.

> #termination phase
> if counter != 0:
>     average = float( totalMilesDriven ) / float( totalGallonsPerTank )
>     print "The total miles per gallon is", average
> else:
>     print "No data entered"

Hope this helps.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



More information about the Python-list mailing list