Monday, April 30, 2012

How to consume command line arguments using raw_input() instead of sys.argv

I am new to python I have developed the following code for some online coding submission. Now my problem is this coding site is not accepting sys.argv. How do I convert this code to take raw_input from command line. raw_input is confusing as compared to sys.argv. Please help. Thanks in advance.



import sys
ln = int(sys.argv[1])
#create list of elements which are sum of two command line input
list = []
i = 2
while i <= ln:
sum = int(sys.argv[i]) + int(sys.argv[i+1])
list.append(sum)
i = i + 2
#update list with elements which are sum of the two elements within the list
j = 0
while j < len(list) - 1:
list.append(list[j]+list[j+1])
j = j + 2
#get final sum using addition of all the elements in the list
k = 0
sum = 0
while k < len(list):
sum = sum + list[k]
k = k + 1
print sum




No comments:

Post a Comment