William
William

Reputation: 83

Python | TypeError : 'float' object cannot be interpreted as an integer

I have tried to create a modularized program (as one of some practice assignments out a book), that asks a user for speed (mph) and time traveled (hrs), then (using a loop) I am trying to display the distance the vehicle has travelled, for each hr of the journey.

I am trying to structure my module in an modularized fashion, hence all the def

Issue: When debugging, I'm getting an TypeError: 'float' object cannot be interpreted as an integer,

def userJourneySpeed():
    vehicleSpeed = float(input("Please enter the speed of your vehicle: "))
    return vehicleSpeed

def userJourneyTime():
    hoursTraveled = float(input("Please enter how many hours you traveled: "))
    return hoursTraveled

def printSpec(hoursTraveled, vehicleSpeed):
    print("Hour", "\tDistance Traveled")
    for currentHour in range(1, hoursTraveled + 1):
        userDistanceTraveled = vehicleSpeed * currentHour
        print(currentHour, "\t", userDistanceTraveled)

def mainProgram():
    vehicleSpeed = userJourneySpeed()
    hoursTraveled = userJourneyTime()
    printSpec(hoursTraveled, vehicleSpeed)
mainProgram()

Any advice on where I have gone wrong, would be greatly appreciated!

Upvotes: 1

Views: 2767

Answers (2)

Xero Smith
Xero Smith

Reputation: 2076

The error stems from trying to get the range of a float. To avoid that, you have to account for floats in the print_spec function.

Writing programs in a modular fashion is very good practice. kudos to you. Something to bear in mind, a key aspect of modularity is to group similar operations together. With this in mind, it makes sense to collect user data in a single function. This way your program maintains its modularity and is less fragmented.

In the spirit of encouraging good practices, documenting your code is a great idea. Adding docstrings at the top of your functions that specify the expected arguments, retrn type and a general overview of the functions purpose is great practice, it helps a lot in understanding and debugging your code.

You can refere to pep8 for more great tips on python style.

Below is your code with some minor changes to get it working as you intend. I have included the term Cumulative Distance Traveled to the print out because that is what you actually calculate each hour.

Further more, I have included fnctionality to handle cases whith fractional hours.

def get_journey_data():
    """(NoneType) -> tuple(float, float)
    Get the speed and duration of travel
    """
    vehicle_speed = float(input("Please enter the average travel speed: "))
    hours_traveled = float(input("Please enter the travel time: "))
    return vehicle_speed, hours_traveled

def print_spec(travel_time, average_speed):
    """(float, float) -> NoneType
    Print the journey table in the following format:
    Current Hour | Distance Traveled
    """
    travel_time_hours = int(travel_time) #get whole hours from 
                                         #travel_time
    travel_time_rem = travel_time % 1 #get fraction of an hour left over
    hline = "\t---------------------------------------------"
    print("\n\tCurrent Hour\t|\tCumulative Distance Traveled")
    print(hline)
    for current_hour in range(1, travel_time_hours + 1):
        print("\t\t", current_hour, "\t|\t", current_hour * average_speed)
        print(hline)
    if travel_time_rem:
        print("\t\t", travel_time_hours + 1, "\t|\t", 
              travel_time*average_speed)
        print(hline)
    return None



def main_program():
    """(NoneType) -> Nonetype"""
    vehicle_speed, hours_traveled = get_journey_data()
    print_spec(hours_traveled, vehicle_speed)
    return None

Upvotes: 0

jwqz
jwqz

Reputation: 103

hoursTraveled is converted to a float from your input.

It is then used in the range function in printSpec. As it is a float, the range function throws the error you provided.

You could solve this by converting the input for hoursTraveled to an int right away, or doing so in the second argument of the range function.

For a quick example try this:

>>> range(1, 2.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got float.

Upvotes: 1

Related Questions