# 在命令提示符中使用 sys.argv 列表

##### 问题描述投票：0回答：3

``input("Enter data")``

``sys.argv``
，它就不起作用了。有什么建议吗？

``````import sys
try:

listOfNumbers = list(sys.argv[1])
listOfEvenNumbers = []
listOfOddNumbers = []
for x in listOfNumbers:
if x % 2 == 0:
listOfEvenNumbers.append(x)
elif x % 2 != 0:
listOfOddNumbers.append(x)

countOfEvenNumbers = len(listOfEvenNumbers)
countOfOddNumbers = len(listOfOddNumbers)
sumOfEvenNumbers = sum(listOfEvenNumbers)
sumOfOddNumbers = sum(listOfOddNumbers)
highestNumber = max(listOfNumbers)
lowestNumber = min(listOfNumbers)
differenceBetweenNumbers = highestNumber - lowestNumber
sumOfCenteredNumbers = sum(listOfNumbers) - highestNumber - lowestNumber
countOfCenteredNumbers = len(listOfNumbers) - 2
centeredAverage = sumOfCenteredNumbers/countOfCenteredNumbers
print("The sum of all even numbers is %i, the sum of all odd numbers is %i,the difference between the biggest and smallest number is %i, the total number of even numbers is %i, the total number of odd numbers is %i,the centered average is %i.")% (sumOfEvenNumbers, sumOfOddNumbers, differenceBetweenNumbers, countOfEvenNumbers, countOfOddNumbers, centeredAverage)

except:
``````

``````evenOddCalculator.py 1,2,3,4
``````

``````Your input is invalid!
``````
python list command arguments system
##### 3个回答
0

``````listOfNumbers = list(sys.argv[1].split(','))
``````

0

``````strOfNumbers = sys.argv[1]
listOfNumbers= [int(x) for x in strOfNumbers.split(',')]
``````

0

``````import sys
try:

listOfNumbers_str = list(sys.argv[1:])
#print listOfNumbers_str
listOfEvenNumbers = []
listOfOddNumbers = []
listOfNumbers = [int(i) for i in listOfNumbers_str]
#print listOfNumbers

for x in listOfNumbers:
if x % 2 == 0:
listOfEvenNumbers.append(x)
elif x % 2 != 0:
listOfOddNumbers.append(x)

countOfEvenNumbers = len(listOfEvenNumbers)
countOfOddNumbers = len(listOfOddNumbers)
sumOfEvenNumbers=sum(listOfEvenNumbers)
sumOfOddNumbers=sum(listOfOddNumbers)
highestNumber=max(listOfNumbers)
lowestNumber=min(listOfNumbers)
differenceBetweenNumbers = highestNumber - lowestNumber
sumOfCenteredNumbers = sum(listOfNumbers) - highestNumber - lowestNumber
countOfCenteredNumbers = len(listOfNumbers) - 2
centeredAverage = sumOfCenteredNumbers/countOfCenteredNumbers
print("The sum of all even numbers is %i, the sum of all odd numbers is %i,the difference between the biggest and smallest number is %i, the total number of even numbers is %i, the total number   of odd numbers is %i,the centered average is %i.")% (sumOfEvenNumbers,sumOfOddNumbers,differenceBetweenNumbers,countOfEvenNumbers,countOfOddNumbers,centeredAverage)

except:
``````

1. sys.argv[1:]

1. listOfNumbers = [int(i) for i in listOfNumbers_str]

sys.argv 返回一个字符串元素数组而不是整数，我在这里使用列表理解从字符串列表中构建一个整数列表。

``````\$ python calc.py 1 2 3 4
['1', '2', '3', '4']
[1, 2, 3, 4]
The sum of all even numbers is 6, the sum of all odd numbers is 4,the difference between the biggest and smallest number is 3, the total number of even numbers is 2, the total number of odd numbers is 2,the centered average is 2.
``````