Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Divide by 0 error kethyar Unladen Swallow Posts: 1 Threads: 1 Joined: Jul 2017 Reputation: 0 Likes received: 0 #1 Jul-16-2017, 04:10 PM (This post was last modified: Jul-16-2017, 04:59 PM by kethyar.) I am having issues with my class assignment. My simple program is having a user input car speed, time traveled and tank capacity. However, when I go to calculate distance traveled (distance = input_speed * input_time) and then calculate MPG (mpg = distance / input_capacity), I am getting a divide by zero error. All of my previous inputs are listed as input_capacity = int(input("Input capacity: ")), but it is obviously not treating the number as an integer or a real number (in the case of using float for time). Can someone help me identify what I might be doing wrong or where I might need to tell the program to treat the input as a number? Sample input and my calculation formulas below: ```def capacity_prompt():    input_capacity = 0    input_capacity = int(input("Enter the number of gallons or liters used: "))    return input_capacity def calculate_distance(input_speed, input_time):    result_distance = 0    result_distance = input_speed * input_time    return result_distance def calculate_mpg(result_distance, input_capacity):    result_mpg = 0    result_mpg = result_distance / input_capacity    return result_mpg ``` Bah, found my own error. I wasn't calling the functions at the end/beginning, I was calling the results. Called the correct variable and now it works fine. Quote Quote snippsat Posts: 1,412 Threads: 54 Joined: Sep 2016 Reputation: 72 Likes received: 426 #2 Jul-16-2017, 05:55 PM (Jul-16-2017, 04:10 PM)kethyar Wrote: I might need to tell the program to treat the input as a number? Yes,that's the first step. So if input is `a` or `1.0`,get a error. ```>>> input_capacity = int(input("Enter the number of gallons or liters used: ")) Enter the number of gallons or liters used: a`````````Error:Traceback (most recent call last):  File "", line 1, in ValueError: invalid literal for int() with base 10: 'a' invalid literal for int() with base 10: 'a'``````This `ValueError` can be catch and give a more useful message to user. ```def capacity_prompt():    while True:        try:            input_capacity = int(input("Enter the number of gallons or liters used: "))            return input_capacity        except ValueError:            print('Only number as input,try again') print(capacity_prompt())```If you run it,you see that it will only return out integer. You use the same method with divide by zero error. `>>> 7 / 0```````Error:Traceback (most recent call last):  File "", line 1, in ZeroDivisionError: division by zero division by zero`````` buran likes this post Quote Quote « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)