do you guys help newbies??
Dennis Lee Bieber
wlfraed at ix.netcom.com
Wed Nov 27 16:13:26 EST 2002
malik m fed this fish to the penguins on Wednesday 27 November 2002
03:48 am:
> #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."
>
You probably weren't expecting an English lesson either, but that
intro is running "... me ... your ... the user ... his/her ...";
implies three different people are involved <G>
You are also assuming that one always runs the tank empty before
filling it, rather than using the amount of fuel needed to refill the
tank.
> #initialize variables
> totalMilesDriven = 0 #total miles driven
>
> totalGallonsPerTank = 0 #total gallons per tank
>
> counter = 0 #bogus counter
>
Definitely bogus, if the only use is to see if the user entered real
values you could just see if the totals are > 0.
>
> while gallonsPerTank or milesDriven != -1:
That is equal to
(gallonsPerTank) or (milesDriven != -1)
Comparisons don't distribute so it is NOT
(gallonsPerTank != 1) or (milesDriven !=1)
which is still incorrect, since someone could enter -1 for ONE item,
but the other could be a positive number, and would make the statement
true.
> gallonsPerTank = raw_input("Enter how many gallons your tank
> holds, -1 to end:")
> milesDriven = raw_input("enter how many miles driven, -1 to end:")
>
Forgot to convert from string to numeric.
> #termination phase
> if counter != 0:
This looks like a homework assignment, and I'm probably going to
violate conventions (don't do their homework). Of course, by the time
my message shows up, you'll have seen all the line-by-line critiques.
,----[ /home/wulfraed/t.py ]
| import string # probably not needed in the newest versions of
Python
| # strings have methods so just variable.upper()
| # could replace string.upper(variable)
|
| #chapter 3 test control structures 3.3
| # as recreated by dlbieber/nov 20 2002
|
| intro = """
| Hello. This program will tell you how many miles per gallon your
| car produces by dividing the amount of miles entered by the
| amount of fuel needed to refill the tank. The tank has to
| be full at the start of the trip!
| """
|
| # initialize stuff
| sumMiles = 0.0
| sumGallons = 0.0
|
| def ProcessSegment():
| global sumMiles #declare the running totals as external to
| global sumGallons #the function
|
| EOI = 0 #initialize End Of Input to false
|
| userin = raw_input("Enter the miles driven in this segment (Q to
end) ")
| if string.upper(userin) != "Q":
| miles = float(userin) # note: no check for invalid input is
| made
| userin = raw_input("Enter the numbers of gallons needed to
refill (Q to end) ")
| if string.upper(userin) != "Q":
| gallons = float(userin) # note: no check for invalid input
| print "Miles per gallon for this segment: %s" % (miles /
gallons)
| sumMiles = sumMiles + miles
| sumGallons = sumGallons + gallons
| else:
| EOI = 1
| else:
| EOI = 1 #set end of input true
|
| return EOI #return the End Of Input flag value
|
|
|
| if __name__ == "__main__":
| print intro
|
| while not ProcessSegment(): #loop until the function returns EOI
true
| print "Average miles per gallon: %s" % (sumMiles / sumGallons)
| print " "
|
| if sumGallons and sumMiles:
| print "\n\nFinal average miles per gallon: %s" % (sumMiles /
sumGallons)
| else:
| print "*** No data entered ***"
`----
--
> ============================================================== <
> wlfraed at ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed at dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <
More information about the Python-list
mailing list