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. Second try.

I posted this earlier and the answer I got back was that because I have number = 0 and that's why I'm getting the error. I thought because I have asked for a number input then shouldn't that be used as the number divisor? Please, give me some clues. I tried placing number = get_number() but that didn't work either.

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)/int(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()

3 Answers

The function display_info is called before the get_number function. This therefore means that the variable "number" is set to 0 during the function display_info call. You will end up getting a ZeroDivisionError: float division by zero from from the expression "total_miles = float(ending_milage - beginning_milage)/ int(number)".

Ok, so can you tell me how to fix it? I tried naming number = get_number() but that didn't change the result. I tried not having int, in front of the equation. In other programs this method of having a number and requesting input for a number and then using whatever the input number for whatever equation. I don't understand why it isn't working this time.

def main(): print("Welcome to the gas and mileage tracker.")

# number of months
months = number_of_months()

# get tank capacity
tank_capacity = int(input("Please enter the volume of you car's tank in gallons: "))

# get gas used in a month
gas_per_month = int(input("Please enter the number of gallons used per month: "))

# get miles travelled on a full tank
miles_on_full_tank = int(input("Please enter the miles covered using a full tank of gas: "))

# get cost of gas per gallon
cost_per_gallon = int(input("Please enter the cost of gas per gallon: "))

# calculate mpg
mpg = get_mpg(miles_on_full_tank, tank_capacity)

# calculate the cost of gas per month
gas_cost_in_a_month = calc(gas_per_month, cost_per_gallon)

# calculate the cost of gas for months
gas_cost_for_months = calc(months, gas_cost_in_a_month)

# calculate distance
distance = mpg * gas_cost_for_months

# display info
display_info(gas_cost_for_months, distance, mpg)

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

def calc(foo, foobar): return foo * foobar

def get_mpg(foo, foobar): return foo / foobar

def display_info(cost, miles_travelled, mpg): print("Your total cost will be: ", cost) print("Your total miles traveled will be: ", miles_travelled) print("Your miles per gallon were: ", mpg)

if name == 'main': main()

I don't understand what foo and foobar are. I've never used those before.

def calc(foo, foobar): return foo * foobar

foo and foobar are parameters for the function calc that returns the product of two values. You can rename them to any name that you like.