我的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:
print("Your input is invalid!")
这是命令提示符中使用的行:
evenOddCalculator.py 1,2,3,4
它给出了结果:
Your input is invalid!
答案 0 :(得分:0)
您的输入无效,因为您的列表中也包含逗号(',')。 要删除它们,您可以使用以下行:
listOfNumbers = list(sys.argv[1].split(','))
答案 1 :(得分:0)
我这样做是为了工作:
strOfNumbers = sys.argv[1]
listOfNumbers= [int(x) for x in strOfNumbers.split(',')]
答案 2 :(得分: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:
print("Your input is invalid!")
我改变了几件事 -
从索引1到结尾
提供所有元素sys.argv返回一个String元素数组而不是整数,我在这里使用list comprehension来构建String列表中的整数列表。
运行程序 - >
$ 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.