Error in Python Script regarding str(sys.argv[1])

…衆ロ難τιáo~ 提交于 2019-12-11 11:41:42

问题


import MySQLdb as mdb
import math
import sys 
from decimal import Decimal

conn=mdb.connect('localhost','root','','testdb')
c=conn.cursor()

user_id=str(sys.argv[1])
lati=str(sys.argv[2])
longi=str(sys.argv[3])
radius=int(str(sys.argv[4]))

#user_id=1
#lati='28.635308'
#longi='77.224960'
#radius=1.5

The error is :

Traceback (most recent call last):
  File "C:\Python27\recommend.py", line 9, in <module>
    user_id=str(sys.argv[1])
IndexError: list index out of range

However the dummy values which is commented by # are working fine. Any help in resolving this error ?


回答1:


One way to checking the mistake you can print out the sys.argv list and check the number of elements it has. The output of the error was saying there is only one element (most probably it's the script's itself -script name-) in the sys.argv. Most probably when you are running the script you don't supply the arguments(you must supply 4 arguments).



来源:https://stackoverflow.com/questions/36646992/error-in-python-script-regarding-strsys-argv1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!