SharedClass Samples / calculate-drive-time-with-cmd-line-arguments.pyOpen in CoCalc
import sys

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument('--distance', type=float, help='The distance to be traveled')
parser.add_argument('--time-to-save', type=float, required=True, help='The number of minutes you want to save')
parser.add_argument('--speed-limit', default=70, type=float, help='The speed you normally travel')

arguments = parser.parse_args()

if arguments.distance == None:
    distance_to_travel = float(raw_input('How far are you going? '))
else:    
    distance_to_travel = arguments.distance
    
if distance_to_travel <= 0:
    print 'The distance to travel should be positive'
    sys.exit ()
    
#distance_to_travel = float(raw_input('How far are you going? '))
#speed_limit = float(raw_input('What is the speed limit? '))
#minutes_to_save = float(raw_input('How many minutes do you want to save? '))

speed_limit = arguments.speed_limit
time_to_save = arguments.time_to_save

if time_to_save > distance_to_travel / speed_limit * 60:
    print 'The value for time to save must be less than the time it takes normally'
    sys.exit()

numerator = (speed_limit * time_to_save * speed_limit) / (distance_to_travel * 60)
denominator = 1 - (speed_limit * time_to_save) / (distance_to_travel * 60)
new_speed = (numerator / denominator) + speed_limit

print 'You should go', new_speed, 'to save', time_to_save, 'minutes over going', speed_limit,
print 'when traveling', distance_to_travel, 'miles'