I've been working on this code all night and i can't seen to find out how to do the math i think it needs a loop of some kind on line 17 and 18 also it wont print out the avg mpg please help


def main():
1 print("This program calculates fuel efficiency over a multi-leg journey.")
2 print("You should enter the gallons of gas consumed and miles traveled")
3 print("for each leg. Just hit <Enter> to signal the end of the trip.")
4 print()
5
6
7 total_distance, total_fuel = 0.0, 0.0
8 inStr = input("Enter gallons and miles (with a space between): ")
9 while inStr != "":
10 gallons,miles = inStr.split()
11 gallons = eval(gallons)
12 miles = eval(miles)
13
14 #This next line should print just the miles per gallon for the leg of the trip 15 you just received from the user
16 print("MPG for this leg: {0:0.1f}".format(miles/gallons))
17 total_distance = miles + miles
18 total_fuel = gallons + gallons
19 inStr = input("Enter gallons and miles (with a space between): ")
20
21
22 print()
23 #This section should print the TOTAL distance & TOTAL fuel used during the trip
24 print("You traveled a total of {0:0.1f} miles on {1:0.1f} gallons."
25 .format(total_distance, total_fuel))
26 #This section should print the average based on the TOTAL distance & TOTAL fuel 27 27 used during the trip
28 print("The fuel efficiency was {0:0.1f} miles per gallon."
29 .format(total_distance/total_fuel))
30
31 if __name__ == '__main__':
32 main()

Recommended Answers

All 6 Replies

It works fine for me. Here is the code I have been using.

def main():
    print "This program calculates fuel efficiency over a multi-leg journey."
    print "You should enter the gallons of gas consumes and miles traveled"
    print "for each leg. Just hit <Enter> to signal the end of the trip."
    print ""


    total_distance, total_fuel = 0.0, 0.0
    inStr = input("Enter gallons and miles(with a space between): ")
    while inStr != "":
        gallons, miles = inStr.split()
        gallons = eval(gallons)
        miles = eval(miles)

        print "MPG for this leg: {0:0.1f}".format(miles/gallons)
        total_distance = miles + miles
        total_fuel = gallons + gallons
        inStr = input("Enter gallons and miles(with a space between): ")


        print ""

        print "You traveled a total of {0:0.1f}miles on {1:0.1f}gallons.".format(total_distance, total_fuel)
        print "The fuel efficiency was {0:0.1f} miles per gallon.".format(total_distance/total_fuel)

main()

Both the above code cannot work properly. The reason is simple, the math is not done properly. Line 16: total_distance = miles + miles and line 17: total_fuel = gallons + gallons In both these cases you are just multiplying the no. of miles and no. of gallons the user has input and storing it in total_distance and total_fuel. Modify it so that you add the no. of miles and gallons the user input to total_distance and total_fuel.

britanicus I was not fixing the math. He had an issue of the code not printing a value that is what I fixed. If you come here to have your math checked you are in the wrong place.

Also, you should check that numbers were entered on input and ask the use to enter again if there was an error. The program now will error out if you try to eval an entry that is not a number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.