python 来自命令行的两个数字的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1356348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sum of two numbers coming from the command line
提问by geeta
I know it's a very basic program but I am getting an error of list out of range. Here is the program to take two numbers as command-line arguments (while invoking the script) and display sum (using python):
我知道这是一个非常基本的程序,但我收到列表超出范围的错误。这是将两个数字作为命令行参数(在调用脚本时)并显示总和(使用 python)的程序:
import sys
a= sys.argv[1]
b= sys.argv[2]
sum=str( a+b)
print " sum is", sum
回答by
You should do this:
你应该做这个:
import sys
a, b = sys.argv[1:2]
summ = int(a) + int(b)
print "sum is", summ
There is no need for str() when printing an integer. But you should use int() if you want to add a and b as integers.
打印整数时不需要 str() 。但是,如果要将 a 和 b 添加为整数,则应使用 int()。
回答by codeape
The error list index out of range
means that you are trying to access a list item that is outside the bounds of the list.
该错误list index out of range
意味着您正在尝试访问列表边界之外的列表项。
Example:
例子:
>>> mylist = ['hello', 'world']
>>> print mylist[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
In your case, the error comes from either sys.argv[1]
or sys.argv[2]
.
在您的情况下,错误来自sys.argv[1]
或sys.argv[2]
。
Make sure you actually pass something to the program from the command line.
确保您确实从命令行向程序传递了一些东西。
回答by David
Assuming your inputs are integers:
假设您的输入是整数:
import sys
summ = sum(map(int,sys.argv[1:])
print "sum is ", summ
or
或者
import sys
summ = sum(int(i) for i in sys.argv[1:])
print "sum is ", summ
If not, change int to float.
如果不是,请将 int 更改为 float。
The second method is probably more pythonic, but the first is a little faster in this instance.
第二种方法可能更像pythonic,但在这种情况下第一种方法要快一些。
>>>import timeit
>>>t1 = timeit.Timer("sum(map(int,['3','5','7','9']))")
>>>t2 = timeit.Timer("sum(int(i) for i in ['3','5','7','9'])")
>>>print t1.timeit()
3.0187220573425293
>>>print t2.timeit()
3.4699549674987793
回答by SilentGhost
su = int(a) + int(b)
print("sum is %d" % su)
And you should be careful with your variable naming. sum
shadows the built-in, it's not a good practice to do that (that is, don't name variables as built-in functions or any globally defined name).
你应该小心你的变量命名。sum
隐藏内置函数,这样做不是一个好习惯(也就是说,不要将变量命名为内置函数或任何全局定义的名称)。
回答by Daniel Pryden
If your error is "list index out of range", then the problem is that your listdoesn't have enough items. Which list? The only list you're using is sys.argv
, so to populate it, you need to pass more items on the command line.
如果您的错误是"list index out of range",那么问题在于您的列表没有足够的项目。哪个名单?您使用的唯一列表是sys.argv
,因此要填充它,您需要在命令行上传递更多项目。
Alternatively, check for the length of the argument list with len(sys.argv)
and prompt interactively (e.g. using raw_input()
) to get the values if they're not in sys.argv
.
另外,检查参数列表,长度len(sys.argv)
和及时交互(例如使用raw_input()
),如果他们在不是得到的值sys.argv
。
回答by Francesco
If you wish to sum the numbers as floating points number use "float" instead of "int", like in the following snippet.
如果您希望将数字求和为浮点数,请使用“float”而不是“int”,如下面的代码片段所示。
import sys
try:
a, b = sys.argv[1:3]
print "sum is ", float(a) + float(b)
except ValueError:
print "please give me two numbers to sum"
Beware that floating points are different from real numbers, so that you could obtain seemingly "strange" results about which there is a wealth of documentation on the web.
请注意浮点数与实数不同,因此您可能会获得看似“奇怪”的结果,网络上有大量关于这些结果的文档。
回答by geeta
Thanks to every one. I got the answer
谢谢大家。我得到了答案
for i in range (1,51): if i%5==0: print i,"\n" else: print i,
对于 i in range (1,51): if i%5==0: 打印 i,"\n" else: 打印 i,