I'm so new to programming logic I'm not sure I can phrase this properly. I'm using Python 2.7 and am trying to write a script that will repeat until zero is entered. I've tried if, else, and while statements and have concluded that I don't know enough about logic to know anything about Python. For example...I'm so new that init means nothing to me. I've seen the phrase in almost every search result I've received, but I don't no what it means or is. The class I'm in is a LOGIC class not a Python class. I can write it in pseudocode but I would really like to see a working model. Please Help. This script runs through and will exit when zero is entered, but it will not prompt for miles driven again.
#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
getMiles = float(input ('Enter Miles: '))
while getMiles == 0:
print "END OF PROGRAM"
exit
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
while costOfTrip == float:
getMiles
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
Something that I forgot to mention was the mandatory "END OF PROGRAM" statement. I used a combination of your answers and this works. Again, Thank you everyone. I can stop banging my head on the wall.
#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or enter zero to quit"
getMiles = float(raw_input ('Enter Miles: '))
while getMiles >= 0:
if getMiles == 0:
print "END OF PROGRAM"
exit()
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
getMiles = float(raw_input ('Enter Miles: '))
-
Just slow down and study your course material when you don't understand something. For instance, can see that you are trying to make intuitive leaps in your code. Consider that programming and the study of logic aren't going to be easy to intuitively understand without a solid foundation of first principles - and that just comes with time and exposure to the ideas. Hang in there, it's an awesome career if you have some solid perseverance.dwerner– dwerner2015年02月03日 05:38:37 +00:00Commented Feb 3, 2015 at 5:38
-
Thank you, you are right. Some things that sounded like a foreign language a week ago are already starting to sound familiar. I really would have preferred just learning logic, but the professor insists we get into python as well. Thanks for the encouragement.Kirk Stevenson– Kirk Stevenson2015年02月03日 06:14:36 +00:00Commented Feb 3, 2015 at 6:14
-
Note that in your revised version, the if statement is not needed in the loop. the "end of program" print can be after the loop (and not indented into the loop's scope). then you won't need the redundant if and the call exit (because the print would be the last thing in the program anyway)Eran– Eran2015年02月03日 09:08:20 +00:00Commented Feb 3, 2015 at 9:08
-
@KirkStevenson - Quite often, it is much easier to learn about abstract concepts (like logic) by interacting with them. You can just as easily sit down with another human being, but humans tend to be quite subjective in their understanding of the truth. Programming languages offer a (relatively) infallible source of truth with which you can interact. In that vein, and due to it's popularity, python is a perfectly reasonable choice for your instructor. (Sorry for going so far off topic of the forum here). More questions in this vein belong on programmers.stackexchange.comdwerner– dwerner2015年02月03日 19:24:33 +00:00Commented Feb 3, 2015 at 19:24
-
2Check the Help Center first. Language choices and educational advice are both off-topic there.Robert Harvey– Robert Harvey2015年02月03日 19:29:13 +00:00Commented Feb 3, 2015 at 19:29
5 Answers 5
Python is not really that far from pseudocode, and indeed the problem here is not the code but the logic.
To get the basic "loop until entering zero" you can have the following logic:
miles = -1
while miles != 0:
miles = float(raw_input ('Enter Miles: '))
As for your own code, you seem to be using 'while' when you mean 'if' And in the second while you are actually just naming a variable (getMiles) which does nothing
The whole code could look like this:
miles = float(raw_input ('Enter Miles: '))
while miles != 0:
fuelEcon = miles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = miles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = miles * fuelIncrease
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
miles = float(raw_input ('Enter Miles: '))
** no need to use "while true" as others suggested, it's never a nice thing to do.
A more advanced version would be to extract the part of the logic which is repeatable and standalone to a function
def trip_cost(miles):
if(miles == 0):
return False
fuelEcon = miles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = miles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = miles * fuelIncrease
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
return True
while trip_cost(float(raw_input ('Enter Miles: '))):
pass
As for what init is, that's a much more advanced topic of Objects Orientation which you probably shouldn't worry about just yet
7 Comments
while func(): pass is actually a lot more confusing and not Pythonic at all.I'd just say as a matter of style that even your new loop condition / logic could be quickly tidied up to read
print "To Calculate the cost of your trip,"
print "enter the miles driven or enter zero to quit"
getMiles = float(raw_input ('Enter Miles: '))
while getMiles != 0:
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
getMiles = float(raw_input ('Enter Miles: '))
print "END OF PROGRAM"
# should be no need for exit() unless code is included in a subroutine
Comments
You were very close, here is the fixed version:
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
while True:
getMiles = float(input ('Enter Miles: '))
if getMiles == 0:
print "END OF PROGRAM"
exit()
fuelEcon = getMiles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = getMiles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = getMiles * fuelIncrease
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
I added a while True around the whole block of code, this will cause the question to be asked over and over again (forever) until the user enters 0 for miles.
The only other thing that needed to be fixed was that exit is a function call so it should be exit().
1 Comment
total_miles = 0
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
getMiles = float(input ('Enter Miles: '))
while getMiles != 0:
total_miles = getMiles + total_miles
getMiles = float(input ('Enter Miles: '))
else:
print "END OF PROGRAM"
exit
fuelEcon = total_miles / 20
fuelCost = float(input ('Enter Cost of Fuel: $'))
costOfTrip = total_miles * fuelCost
fuelIncrease = (fuelCost * .1) + fuelCost
futureTrip = total_miles * fuelIncrease
while costOfTrip == float:
getMiles
print "Cost of Trip: $", costOfTrip
print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip
Comments
You could do it like this
#Cost of Trip Ch2 Q8
print "To Calculate the cost of your trip,"
print "enter the miles driven or zero to quit"
while True:
getMiles = float(input ('Enter Miles: '))
if getMiles == 0:
print "END OF PROGRAM"
break
print 'Do the other calculations'
Go into an infinite loop until 0 is entered at which point you break out of the loop and the program ends.
You can use While 1: under python 2.7 to get faster performance, but I doubt this will be your concern at the moment.