[英]Simple sum program in Python
I just wrote this very simple Python script which finds the sum of two integers. 我刚刚编写了这个非常简单的Python脚本,该脚本可以找到两个整数的和。 I'm just trying to understand how the return
values are assigned to variables: 我只是想了解如何将return
值分配给变量:
def add(a,b):
c = a + b
return a
return b
return c
first_number, second_number, result = add(3,4)
print 'first_number is ', first_number
print 'second number is ', second_number
print 'result is ', result
When I try to run this script, I get the following: 当我尝试运行此脚本时,得到以下信息:
Traceback (most recent call last):
File "test.py", line 7, in <module>
first_number, second_number, result = add(3,4)
TypeError: 'int' object is not iterable
Why is that? 这是为什么? And, how can I solve this issue? 而且,我该如何解决这个问题?
Your problem is that the return a
statement stops the routine and returns only one value, but the main program expects three values. 您的问题是, return a
语句将停止例程并仅返回一个值,但是主程序需要三个值。 Your other return statements are never executed. 您的其他return语句永远不会执行。
Instead, use just one return
statement: 相反,仅使用一个return
语句:
def add(a,b):
c = a + b
return a, b, c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.