- #1
rawen
- 6
- 0
Code:
Homework Statement
Basically, I'm supposed to create a simple program that will calculate the position of an object according to the formula y(t) = v0*t - 0.5*g*t**2. Parameters v0 and t are supposed to be read from the command line and if one of them, or both are not given, the program should ask for them via an exception.
Homework Equations
The Attempt at a Solution
----------------------------------------------------------------
Code:
import getopt, sys
g = 9.81
options, args = getopt.getopt(sys.argv[1:], '',['v0=', 't='])
try:
for option, value in options:
if option == '--t':
t = float(value)
elif option == '--v0':
v0 = float(value)
except:
if '--t' not in options:
t = raw_input('t= ?')
t = float(t)
elif '--v0' not in options:
v0 = raw_input('v0= ?')
v0 = float(v0)
y = v0*t - 0.5*g*t**2
print 'y=', y, 'm'
It apparently works if I provide a valid input on a command line, but if I omit one of the parameters, the program is terminated with NameError: name 'v0' is not defined (for example). It completely ignores my except block. I'm new to this so it may be a bit silly, but I'm thankful in advance for any help.
Last edited: