sys.argv导致索引错误

时间:2014-04-07 17:22:10

标签: python api

这是我的泊松过程脚本。它引起的问题是错误,即第7行(a = float(sys.argv[1])

  

IndexError:列表索引超出范围。

我无法修复它。请帮我修理它。

import random
import math
import sys
import time

# Expected number of arrivals per unit time.
a = float(sys.argv[1])
# Number of events
count = int(sys.argv[2])

# Function for testing API
def test_api():
    print "Testing API..."
    # TODO: Make request to the API

# Make a function iterable, by repeatedly calling it.
def make_iterable(func, *args):
    try:
        while 1:
            yield func(*args)
    except:
        pass

uni_rand = make_iterable(random.uniform, 0, 1)

# A generator for inter-arrival times.
inter_arrival = ( -(1./a)*math.log(u) for u in uni_rand)

# Generate inter-arrival times, then sleep for that long.
inter_arrival_iter = iter(inter_arrival)
for i in xrange(count):
    inter_arrival_seconds = inter_arrival_iter.next() * 3600.
    print "Sleeping for %f seconds." % inter_arrival_seconds
    time.sleep(inter_arrival_seconds)
    test_api()

1 个答案:

答案 0 :(得分:2)

你没有“修复它”,你将参数传递给脚本。

./script.py 1.2 7

如果你想事先检查你是否有足够的参数,那么你需要检查它的长度(len(sys.argv))。