Project #06: Calculate Travel Time and Estimated Time of Arrival (ETA) _ Python for Beginners

Опубликовано: 31 Март 2025
на канале: Make Everyday EZ Day
1,795
like

Estimated time of arrival (ETA) is the time when a vehicle is expected to arrive at a certain place.
This python program simulates a travel time calculator. It takes into account factors like distance (length for each road segment), speed limit, and wait time at traffic lights.

Write a Python program to calculateEstimated Travel Timeand Estimated Time of Arrival (ETA)
Your program will consider the following factors:
1. Number of traffic lights
2. Red light and green light time at each traffic light
3. Number of road segments (2 segments meet at a traffic light)
4. Length of each road segment
5. Speed limit on each road segment

When running, the program first asks for inputs:
Number of road segments: n
For each segment, user enters length and speed limit
We can see from the diagram, if number of segments is n, then number of traffic lights must be n-1
So, next, user enters red light and green light time for n-1 traffic lights

We convert data to float (for length) and int (for speed limit) when accepting inputs
Then add them to segments list by append() method

Note:
Each item in segments list is an individual segment
A segment consists of 2 sub items: length and speed limit

To find the estimated travel time, we need to find the estimated wait time at all traffic lights, and the moving time on all segments. Then add them altogether!

We define a function est_wait_time() that takes 2 arguments: red_time and green_time
This function will return the estimated wait time at a specified traffic light

To estimate average wait time, we just need to average the wait time for every moment (every second):

When output the time by Python f-strings formatting, remember to give it the width and padding option.

To find ETA based on current time, we need to import datetime module to use some datetime function

The now() function provides a lot of information: month, date, year, hour, minute, second, millisecond…
So we use the strftime() function to extract only what we need, convert it to string and display as desired.

Functions used in the video:
datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format string
%a Weekday as locale’s abbreviated name.
%A Weekday as locale’s full name.
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
%d Day of the month as a zero-padded decimal number.
%b Month as locale’s abbreviated name.
%B Month as locale’s full name.
%m Month as a zero-padded decimal number.
%y Year without century as a zero-padded decimal number.
%Y Year with century as a decimal number.
%H Hour (24-hour clock) as a zero-padded decimal number.
%I Hour (12-hour clock) as a zero-padded decimal number.
%p Locale’s equivalent of either AM or PM.
%M Minute as a zero-padded decimal number.
%S Second as a zero-padded decimal number.

sum(iterable, /, start=0)
Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

datetime.timedelta
A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.
----------------------------------------
Python Tutorial for Beginners
Python Coding for Beginners
Python Programming for Beginners
Learn Python by Building Projects
Python Exercises and Practical Examples with solutions
Practice Python Online with solution
How to write a Python program
Python Programming Data Structure and Algorithm
Python Problem Solving
Python Game Tutorial
Python Game Programming
Python Game Development
Python Tips and Tricks

tag
travel time between two locations,
How do I find the drive time between two locations,
How do you calculate travel time,
How do you calculate travel distance,
travel time calculator driving,
travel time calculator map,
driving time and distance google,
driving distance calculator,
travel time calculator google maps,
What is the formula for calculating ETA
Estimated Time of Arrival (ETA) Calculator,
traffic light waiting time