這是我的泊松過程腳本。它導致的問題是錯誤即在第7行(a = float(sys.argv[1])
)sys.argv會導致索引錯誤
IndexError: List index out of range.
我沒有能夠解決它。請幫我解決它。
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()
運行腳本時,是否確實傳入了命令行參數? –
好像你沒有提供命令行參數。 – rahul
我將如何傳遞來自主機的參數? –