Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Ok, I keep getting this error, please help. ZeroDivisionError: float division by zero

I had the calculations in the def get_mpg but it wasn't working there either, it would take in some info but then go to the end program statement. Any ideas, on what I messed up on?

 total_miles = float(ending_milage - beginning_milage)/ int(number)
ZeroDivisionError: float division by zero

This is the who;e code...

#Cheri Castro
#14January18
#Lesson 5 Dicussion

def main():
    print("Welcome to the monthly gas and milage tracker.") 
    end_program = 'no'
    counter = 1
    number = 0
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    total_miles = 0.0
    miles_pergal = 0.0
    display_info(counter, number, ending_milage, beginning_milage, total_gal, total_cost, total_miles, miles_pergal)

    while end_program == 'no':
      end_program, counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

      number = get_number()
      miles_pergal = get_mpg(number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal)

      end_program = input("Do you want to end the program?(Enter yes to end the program or no to continue with another calcualtion of miles per gallon).:") 

def get_number():
    number = int(input("How many months worth of gas do you want to calculate?: "))
    return number


def get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal):
    for counter in range(number):
      cost_pergal = float(input("What was the cost per gallon?: "))
      total_gal = float(input("What were the total gallons?: "))
      total_cost = float(cost_pergal)*(total_gal)/(number)
      beginning_milage = float(input("What was the beginning milage?: "))
      ending_milage = float(input("What was the ending milage?: "))
      return number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

def display_info(counter, number, ending_milage, beginning_milage, total_gal, total_cost, total_miles, miles_pergal):
    total_miles = float(ending_milage - beginning_milage)/ int(number)
    miles_pergal = float(total_gal/ total_miles) * int(number)
    print("Your total cost was: " + total_cost)
    print("Your total miles traveled was: " + total_miles)
    print("Your total miles per gallon were: " + miles_pergal)

main()    

2 Answers

Hey Cheri,

I added comments to your code to explain why you are getting that exception! Hope this helps and if you need some more helping continuing with the problem, feel free to get back to me!

#Cheri Castro
#14January18
#Lesson 5 Dicussion

def main():
    print("Welcome to the monthly gas and milage tracker.") 
    end_program = 'no'
    counter = 1
    number = 0
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    total_miles = 0.0
    miles_pergal = 0.0
    display_info(counter, number, ending_milage, beginning_milage, total_gal, total_cost, total_miles, miles_pergal)
    # So you initilize everything at the start of the main and then you call the display_info method
    # Lets jump down to that method

    while end_program == 'no':
      end_program, counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

      number = get_number()
      miles_pergal = get_mpg(number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal)

      end_program = input("Do you want to end the program?(Enter yes to end the program or no to continue with another calcualtion of miles per gallon).:") 

def get_number():
    number = int(input("How many months worth of gas do you want to calculate?: "))
    return number


def get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal):
    for counter in range(number):
      cost_pergal = float(input("What was the cost per gallon?: "))
      total_gal = float(input("What were the total gallons?: "))
      total_cost = float(cost_pergal)*(total_gal)/(number)
      beginning_milage = float(input("What was the beginning milage?: "))
      ending_milage = float(input("What was the ending milage?: "))
      return number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

def display_info(counter, number, ending_milage, beginning_milage, total_gal, total_cost, total_miles, miles_pergal):
    total_miles = float(ending_milage - beginning_milage)/ int(number)
    # So your first calculation, you divide by number at the end
    # But if you look back, number = 0 at the start, so now you are trying to divide by 0
    # which is what is causing your error
    miles_pergal = float(total_gal/ total_miles) * int(number)
    print("Your total cost was: " + total_cost)
    print("Your total miles traveled was: " + total_miles)
    print("Your total miles per gallon were: " + miles_pergal)

main()

Ok, I see but since I ask for a number to be input, shouldn't it take that input to divide by?

No because you ask for that input 3 lines of code later! The program never reaches a point of asking you for input because it has already tried to divide by 0 and crashed the whole program? Are you following me?

I think so, so what you're saying is that it initiates then skips past get_number and goes straight to display_info and then attempts to complete the equation. I have worked on this some more and was able to get the requests for information for whatever number the user inputs, but it is still not completing the equations, and is just returning all 0's for the results of the equations. So, please see the new code below, to see if you can pinpoint the error now. I tried with and without a while loop and tried it with a for counter in range, in the get_mpg.

def main():
    print("Welcome to the monthly gas and milage tracker.") 
    end_program = 'no'
    counter = 0 
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    total_miles = 0.0
    miles_pergal = 0.0
    number = get_number()
    get_info(cost_pergal, total_gal, beginning_milage, ending_milage, number)
    total_cost, total_miles, miles_pergal = get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal)
    display_info(total_cost, total_miles, miles_pergal)

    while end_program == 'no':
      end_program, counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

      end_program = input("Do you want to end the program?(Enter yes to end the program or no to continue with another calcualtion of miles per gallon).:") 

def get_number():
    number = int(input("How many months worth of gas do you want to calculate?: "))
    return number

def get_info(cost_pergal, total_gal, beginning_milage, ending_milage, number):
    counter = 0
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    for counter in range(number):
      cost_pergal = float(input("What was the cost per gallon?: "))
      total_gal = float(input("What were the total gallons?: "))
      beginning_milage = float(input("What was the beginning milage?: "))
      ending_milage = float(input("What was the ending milage?: "))
    return cost_pergal, total_gal, beginning_milage, ending_milage 

def get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal):
    counter = 0 
    while counter == number:
      total_cost = float(cost_pergal) * float(total_gal)
      total_miles = float(ending_milage) - float(beginning_milage)
      miles_pergal = float(total_gal)/float(total_miles)
      counter = counter + 1
    return total_cost, total_miles, miles_pergal

def display_info(total_cost, total_miles, miles_pergal):
    print("Your total cost was: $ ", total_cost)
    print("Your total miles traveled was: ", total_miles)
    print("Your total miles per gallon were: ", miles_pergal)

main()

Hello again Cheri,

So I'm glad you've moved forward a bit with the code, I will once again comment any issues I see in the code and try help you work through it some more. I am also going to point out some things that arn't breaking the code, but will make the code easier to understand and more readable. One thing I would like to point out, you are using the get_number() function in an attempt to calculate a couple of months at the one time. Unless the problem actually states that it should be completed this way, I would actually remove this and simply calculate one month at a time as this is overly complicating the solution. I would simply do one month at a time and at the end of the while statement just ask "Would you like to calculate another month?" and if the answer is yes, run the program again.

def main():
    print("Welcome to the monthly gas and milage tracker.") 
    end_program = 'no'

    # As stated above i would remove this counter part
    counter = 0

    # All of these do not need to be initilised here
    # Its not code breaking but you are doing it again in the get_info() function
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    total_miles = 0.0
    miles_pergal = 0.0

    # Again as stated above i would remove the get_number() function
    number = get_number()

    # So here is the reason why you are still getting 0's
    # You are calling this function but you are not storing any information
    # In the function below you get all the information from the user
    # But you do not bring those back to this main() function
    # Also you are sending information to the function but you dont need to
    # This function is simply taking user input, it doesnt need any values sent to it
    # So it should look more like this:
    # cost_pergal, total_gal, beginning_milage, ending_milage = get_info()
    get_info(cost_pergal, total_gal, beginning_milage, ending_milage, number)

    # So this one is actually more correct
    # Just again as stated above i would remove the counter and number arguments
    # But again you are sending info to the function that it does not need
    # It should look more like this:
    # total_cost, total_miles, miles_pergal = get_mpg(cost_pergal, total_gal, beginning_milage, ending_milage)
    total_cost, total_miles, miles_pergal = get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal)

    # This is perfect, you have sent exactly the arguments that you need
    display_info(total_cost, total_miles, miles_pergal)

    # This while loop is actually in the wrong place, right now it doesnt really do anything
    # You need to put the methods from above into it so it runs them everytime the user
    # Says no to ending the program
    # For now i would remove this, get the code actually working, and then add it back
    while end_program == 'no':
      end_program, counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal

      end_program = input("Do you want to end the program?(Enter yes to end the program or no to continue with another calcualtion of miles per gallon).:") 

# As stated above, i would remove this
def get_number():
    number = int(input("How many months worth of gas do you want to calculate?: "))
    return number

# Remove the for loop, but keep the inputs in this method and it should run perfectly
# Also like i said in the main(), you dont need the arguments here
# def get_info() is fine
def get_info(cost_pergal, total_gal, beginning_milage, ending_milage, number):
    counter = 0
    cost_pergal = 0.0
    total_gal = 0.0
    total_cost = 0.0
    beginning_milage = 0.0
    ending_milage = 0.0
    for counter in range(number):
      cost_pergal = float(input("What was the cost per gallon?: "))
      total_gal = float(input("What were the total gallons?: "))
      beginning_milage = float(input("What was the beginning milage?: "))
      ending_milage = float(input("What was the ending milage?: "))
    return cost_pergal, total_gal, beginning_milage, ending_milage 

# As i said in the main() function you are taking some arguments here that you dont need
# I would just use cost_pergal, total_gal, beginning_milage, ending_milage
# Remove the while loop while keeping whats inside it and it should work
def get_mpg(counter, number, cost_pergal, total_gal, total_cost, beginning_milage, ending_milage, total_miles, miles_pergal):
    counter = 0 
    while counter == number:
      total_cost = float(cost_pergal) * float(total_gal)
      total_miles = float(ending_milage) - float(beginning_milage)

      # This calculation is the wrong way around, should be total_miles / total_gal
      miles_pergal = float(total_gal)/float(total_miles)

      counter = counter + 1
    return total_cost, total_miles, miles_pergal

# This is perfect
def display_info(total_cost, total_miles, miles_pergal):
    print("Your total cost was: $ ", total_cost)
    print("Your total miles traveled was: ", total_miles)
    print("Your total miles per gallon were: ", miles_pergal)

main()

Phew, okay i know thats probably a lot to take in, but i recommend you read through it carefully and try to work through the problems slowly! I actually have a solution typed up so if you still can't get it I will post my solution and you can try learn from that! But try fix it yourself first as this will help you learn and understand more!

Good luck and happy coding, Aaron

Oh boy, gonna have to read through this tomorrow after work. Thank you for your input. For what was supposed to be a creation of a simple program, I sure have turned it into something complicated. I am having a difficult time creating a program from my head that is simple. I think of an idea but it doesn't meet the criteria of small nor simple. That class ends on Friday and there were supposed to be four programs with discussion posts but they were each only worth five points. I left them for the very last because I've done everything else and figured if I missed out on twenty points then it'd be ok, I hope. The other stuff that had to be turned in were big ticket items, final project (200 points, he hasn't graded it yet) final exam (200 points, I made a 188). Plus, the spring semester has already started yesterday and now I'm trying to get all of those four classes of initial requirements done, while still trying to finish those four small programs that weren't really small (by my versions). Ah well, I'll keep plugging away at this one, I'm still determined to get it to work, even after the deadline has passed.

It's no problem at all! Happy to help! Don't sweat it too much! Trying to organise a problem out logically while you are still learning can be quite tough! Just take a few minutes to think about exactly what you need to do and how you are going to do it. When you have a rough idea then start to build it one piece at a time, test it to make sure that part works and then move on to the next piece! It can be very handy to create your file and start planning it in your code using comments. I see you have not put any comments in your code, I realise that sometimes comments may seem a little pointless when you may be the only person ever reading the code but it helps you work out your own thought process and then if anyone has to read your code, just like i am now, it can make it easier for me to see whats going on inside your head. So just as an example, if i was just starting this project fresh, I would create the file and start working on it like this:

''' This program will take some milage information from the user
    and work out some calculations from the input. I need to :

    - Create a function to accept some input from the user
    - Create a function to do the calculations
    - Create a function to print the results to the screen
    - Add a while loop to main() so the user can run the program multiple times'''

''' This function will allow us to accept input from the user
    We need to get the users :

    - Cost per gallon 
    - Total gallons
    - Beginning milage 
    - Ending milage'''

def get_info():
    # Code goes here
    # Return the four things we stated above

''' This is where the functions of the program will run '''
def main():
    # Get the users input
    cost_pergal, total_gal, beginning_milage, ending_milage = get_info()

    # Test that our first function works
    print(cost_pergal, total_gal, beginning_milage, ending_milage)

''' Run our main() function '''
main()

I'm going very heavy and detailed on the comments here just to show you my thought process when creating the first function, but you could add simpler and smaller comments so you see your own thought process and others can too. Also when you have created the first function, you would write that one test line in to make sure it works. When you are sure that first functions works then you can just remove the test line and start working on the next function!

Hope this helps and as always I'm here if you need anymore help! Best of luck

Thank you for that! We went over creating psuedocode but the way the class material presents doing it, didn't really make alot of sense to me. So, I've mostly avoided doing It at all. The way you just explained it and have example made sense to me. It seemed more simple and easy to understand the way you annotated your comments. I will incorporate that and hopefully it'll help me improve. Thank you very much.