Take the 2-minute tour ×

I am writing my first program in Python and I was getting an error:


This is what I'm doing:

import sys
import math
import scipy.stats import norm

S_t = sys.argv[1];
K = sys.argv[2];
r = sys.argv[3];
T = sys.argv[4];
sigma_0 = sys.argv[5];

d1 = (math.log(S_t/K) + (r - pow(sigma_0, 2)/2)*T)/(simga_0*math.sqrt(T));
x = norm.cdf(d1);

I'm not sure where my mistakes are. Also, is x = norm.cdf(d1) the best way of calculating the cumulative norm?

share|improve this question
Side note: You do not need a ; at the end of each line in Python, and should not use one. (When it's not completely meaningless, it's usually misleading.) –  abarnert Apr 19 '13 at 2:00
Also, in the future, post complete tracebacks, not just the error message. In this case, there's really only one line the error could have happened on, but often that's not true, and you don't want to make people waste time guessing where the error happened when they could be answering you right away. –  abarnert Apr 19 '13 at 2:04
One last thing: You've only got a single value here, so… the cumulative norm of all your values is just that one value, right? Normally, you don't use scipy.stats unless you have some kind of collection (usually a numpy.ndarray) of lots of values. –  abarnert Apr 19 '13 at 2:06
So, something like "import numpy.ndarray import norm" ? –  Josh Apr 19 '13 at 2:07
"unsupported operand for /" means "unsupported arguments for the operator /", that is, you are trying to perform an operation with the / operator (a division), whose operands are of type string. In plain english, this means: "you cannot divide two strings". –  heltonbiker Apr 19 '13 at 2:09
show 2 more comments

1 Answer

up vote 6 down vote accepted

The problem is that S_t and K are strings, because command-line arguments are strings.

If you want to convert them to some other type, you have to tell Python how/what to convert. For example:

S_t = float(sys.argv[1])
K = float(sys.argv[2])
r = float(sys.argv[3])
T = float(sys.argv[4])

Meanwhile, it's worth learning to debug this yourself. When you get an error in a long line like that, and you can't tell what part of it's wrong, break it down. If you've got this:


Try breaking it down into

d1a = math.log(S_t/K)
d1 = d1a + d1b

Now, see whether the error is in d1a or d1b. Since it's in d1a, break it up again:

d1a1 = S_t/k
d1a = math.log(d1a1)

And now you can see it's in d1a1. That's much easier to figure out—and, if you still can't figure it out, you can post a much shorter question here:

import sys

S_t = sys.argv[1]
K = sys.argv[2]

d1a1 = S_t/k
share|improve this answer
Oh thanks, should've figured that! –  Josh Apr 19 '13 at 2:01
add comment

Your Answer


