问题描述
print(result)
在我的 total
函数中没有打印我的结果。 不应该使用 summs
函数将结果值返回给调用它的函数吗?
这是我的代码:
def main():
#用户的年龄和用户最好的朋友的年龄。
firstAge = int(input(Enter your age:))
secondAge = int(input(输入你最好的朋友的年龄:))
total(firstAge, secondAge)
def total(firstAge,secondAge):
(firstAge,secondAge)
print(result)
sum函数接受两个整数参数并将这些参数的总和作为整数返回。
def sum(num1,num2):
result = int(num1 + num2)
返回结果
main()
我使用Python-3.6.1。
它会返回结果,但不会将其分配给任何内容。因此,当您尝试打印它并引发错误时,未定义结果变量。
调整您的总体函数并将返回值和值相加这个例子 response
为了更清楚地说明在 result >总和功能。一旦您将它分配给一个变量,您可以使用该变量进行打印。
def total(firstAge,secondAge):
response = summs(firstAge,secondAge)
print(response)
The print(result)
in my total
function isn't printing my result.
Shouldn't the sums
function return the result value to the function that called it?
This is my code:
def main():
#Get the user's age and user's best friend's age.
firstAge = int(input("Enter your age: "))
secondAge = int(input("Enter your best friend's age: "))
total(firstAge,secondAge)
def total(firstAge,secondAge):
sums(firstAge,secondAge)
print(result)
#The sum function accepts two integers arguments and returns the sum of those arguments as an integer.
def sums(num1,num2):
result = int(num1+num2)
return result
main()
I'm using Python-3.6.1.
It does return the result, but you do not assign it to anything. Thus, the result variable is not defined when you try to print it and raises an error.
Adjust your total function and assign the value that sums returns to a variable, in this case response
for more clarity on the difference to the variable result
defined in the scope of the sums
function. Once you have assigned it to a variable, you can print it using the variable.
def total(firstAge,secondAge):
response = sums(firstAge,secondAge)
print(response)
这篇关于在函数中打印返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!