0

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: '))
5
  • 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. Commented 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. Commented 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) Commented 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.com Commented Feb 3, 2015 at 19:24
  • 2
    Check the Help Center first. Language choices and educational advice are both off-topic there. Commented Feb 3, 2015 at 19:29

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

All of the advice I have received is amazing. Up until 5 minutes ago, I couldn't write this to work and probably spent 3 hours googling and yahooing. Now I have 4 amazingly simple answers to the same question. And they all work. Thank you.
Wait, why is it not nice to use "while true"? I want to be as considerate to those who may have to read the code I write. Is it an etiquette thing? Also what is the purpose of indenting the rest of the code after the while statement? Thanks again.
I wouldn't say "etiquette" but a matter of conventions, writing better/readable code. In simple examples like this it doesn't matter much, but as a habit when you read a loop condition such as "while miles != 0" you immediately know the logic of the loop, its purpose and intention When you read "while True" you have to keep looking inside for the code that breaks-out of the loop and explains what the loop is for. Also, it is more bug prone towards infinite loops and can be harder to debug. Again non of this is set in stone or always right, just good rules of thumb for better programming.
About indentation, in python it signifies the scope of the code. in our case, everything that's indented after the while will be repeated with each iteration of the loop, anything that's not indented will be outside of the loop's scope.
Can you provide some evidence where 'while True' is bad convention? This is first I hear of this. I would add that having a while func(): pass is actually a lot more confusing and not Pythonic at all.
|
1

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

0

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

Thank you so much. This is perfect. You should run before I start asking about defining functions in an imported module.
0

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.