CoCalc Public FilesClass Samples / calculate-drive-time-with-cmd-line-options.py
Author: Ryan McFall
Compute Environment: Ubuntu 20.04 (Default)
1from argparse import ArgumentParser
2
3parser = ArgumentParser()
4
5distance_to_travel = 0
6
7parser.add_argument('--distance', dest='distance_to_travel', type=float, help='The distance you will travel')
8
10
11arguments=parser.parse_args()
12
13if not arguments.distance_to_travel:
14    distance_to_travel = float(input('How far are you going? '))
15else:
16    distance_to_travel = arguments.distance_to_travel
17
18if not arguments.speed_limit:
19    speed_limit = float(input('What is the speed limit? '))
20else:
21    speed_limit = arguments.speed_limit
22
23minutes_to_save = float(input('How many minutes do you want to save? '))
24
25numerator = (speed_limit * minutes_to_save * speed_limit) / (distance_to_travel * 60)
26denominator = 1 - (speed_limit * minutes_to_save) / (distance_to_travel * 60)
27
28new_speed = (numerator / denominator) + speed_limit
29
30print ('You should go', new_speed, 'to save', minutes_to_save, 'minutes over going', speed_limit, end=' ')
31print ('when traveling', distance_to_travel, 'miles')
32