import sys # # A quick method to read in all neccessary parameters. # In the homework assignment you will have to extent it appropriately. # # Note that we use a simple check methods to check whether the inputs are what we # expect, and, if numeric, in a reasonable, physically sane range. # def read_parameters(): """Read cannon shot parameters from the command line.""" if len(sys.argv) == 5: vlaunch = check_float(sys.argv[1], 0.0) angle = check_float(sys.argv[2], 0.0, 180) b2_m = check_float(sys.argv[3], 0.0) dt = check_float(sys.argv[4], 0.0) else: vlaunch = read_float('Launch velocity in ms^-1? (recommended: 700) ', 0.0) angle = read_float('Launch angle in degrees? ', 0, 180) b2_m = read_float('B2/m in m^-1? (recommended: 4e-5) ', 0.0) dt = read_float('Size of timestep in s? ', 0.0) print return vlaunch, angle, b2_m, dt def check_float(value, minimum=None, maximum=None): """Check that a value is a float in the required range""" value = float(value) if minimum is not None and value < minimum: raise ValueError, 'Below minimum.' if maximum is not None and value >= maximum: raise ValueError, 'Above maximum.' return value def read_float(question, minimum=None, maximum=None): """Read a float value from the command line""" while True: try: value = raw_input(question) value = check_float(value, minimum, maximum) break except ValueError, message: print message, '--- Try again.' return value def check_enum(value, choices): """Read an enum string value from the command line""" if not value in choices: raise ValueError, 'Not in list %s.' % str(choices) return value def read_enum(question, choices): """Read an enum string value from the command line""" while True: try: value = raw_input(question) value = check_enum(value, choices) break except ValueError, message: print message, '--- Try again.' return value