I’m currently going through a few tutorials to get myself up and running on Python, but I seem to hit the same problem a few times. The tutorial I’m currently following is Aloha.py in Introduction to Simulation by Norm Matloff.
The problem I’m hitting seems to be in the following code:
import random, sys
class node: # one object of this class models one network node
# some class variables
s = int(sys.argv[1]) # number of nodes
The error message when I try and run the programme is:
Traceback (most recent call last):
File "C:\Python26\Aloha.py", line 8, in <module>
class node:
File "C:\Python26\Aloha.py", line 10, in node
s = int(sys.argv[0])
ValueError: invalid literal for int() with base 10: 'C:\\Python26\\Aloha.py'
I’ve worked out that sys.argv[1] doesn’t exist when I try and run the program, so does anyone know where I might be going wrong? Is there some way of starting the program that will set these values or is my system somehow set up incorrectly?
sys.argv is for collecting the options given to the program on the command-line. So instead of just running the file, you’ll want to run
python aloha.py 5(or whatever number you want).(Otherwise, you could just set the number directly in the code instead of always expecting it on the command-line, as in
s = 5for example.)