1

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!

4
  • If you are getting an error, please post the full error message, inlcuding the stack trace. Commented Oct 11, 2017 at 23:37
  • 1
    That being said, it is coming from you passing a float object to the range function, which requires int objects. Commented Oct 11, 2017 at 23:37
  • Pleased to meet you William, and I hope you enjoy learning Python and being a member of SO. However, we don't put greetings in our posts here. The rule of thumb is: if you wouldn't expect to see it on a Wikipedia page, don't put it in your question (or answer). BTW, that's nice looking code, although it's customary in Python to call the main function main rather than mainProgram, and we use snake_case for function names rather than camelCase. See PEP 8 -- Style Guide for Python Code for further details. Commented Oct 11, 2017 at 23:48
  • @PM2Ring - Thank you for the response. Just like my clean code, I will be cleaner with my posts going forward; very grateful for the hints / tips on the proper etiquette on SO. Commented Oct 12, 2017 at 0:12

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, makes complete sense, now I think about it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.