在命令提示符下使用 sys.argv 列表
Posted
技术标签:
【中文标题】在命令提示符下使用 sys.argv 列表【英文标题】:Using sys.argv for lists in command prompt 【发布时间】:2018-03-02 03:18:13 【问题描述】:我的带有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!
【问题讨论】:
试试 evenOddCalculator.py 1 2 3 4,空格不是逗号。 不,它给出了相同的结果。 因此,一般来说,将一大段代码包装在try-except
中并不好,因为您永远无法确定哪里异常来自。如果你有一个“bare except”子句,即使是 moreso。尝试使用try-except
包装最少的代码,并明确捕获您期望的异常,例如except ValueError:
、except KeyError
等...
【参考方案1】:
这是工作代码 -
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!")
我改变了几件事 -
-
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.
【讨论】:
【参考方案2】:我这样做是为了让它工作:
strOfNumbers = sys.argv[1]
listOfNumbers= [int(x) for x in strOfNumbers.split(',')]
【讨论】:
【参考方案3】:您的输入无效,因为您的列表也包含逗号 (',')。 为了删除它们,您可以使用以下行:
listOfNumbers = list(sys.argv[1].split(','))
【讨论】:
它仍然给出结果:“您的输入无效!” @SarahCollins 调试起来有点困难,因为你有这么大的“尝试”条件。将 list(sys.argv[1]) 放入其中后 listOfNumbers 包含什么? 这就是我所做的。程序代码:strOfNumbers = (sys.argv[1]) listOfNumbers = list(strOfNumbers) print listOfNumbers 命令提示行 \evenOddCalculator.py" 12345 ['1', '2', '3', '4', '5 '] 您的输入无效! @SarahCollins 很抱歉,但不是很清楚。 listOfNumbers 里面是否包含字符串 "\evenOddCalculator.py" 12345 ['1', '2', '3', '4', '5'] "? 它没有。我的意思是我在命令提示符下运行了以下代码。 "\evenOddCalculator.py" 12345 ['1', '2', '3', '4', '5'] "以上是关于在命令提示符下使用 sys.argv 列表的主要内容,如果未能解决你的问题,请参考以下文章