Python函数中变量值展示方法 - 使用print函数
可以使用print函数来展示调用的函数中变量的值。例如:
def calculate_sum(num1, num2):
total = num1 + num2
print('The sum of', num1, 'and', num2, 'is', total)
return total
result = calculate_sum(3, 5)
在上面的例子中,我们定义了一个函数calculate_sum,该函数接受两个参数num1和num2,计算它们的和并保存在变量total中。然后,我们使用print函数来展示调用该函数时传入的参数和计算出的总和。最后,我们将计算出的总和保存在变量result中。当我们调用该函数时,它将输出以下内容:
The sum of 3 and 5 is 8
这表明我们成功地展示了调用函数中变量的值。
原文地址: https://www.cveoy.top/t/topic/oJSp 著作权归作者所有。请勿转载和采集!