Python编码(调用函数/返回值) [英] Python Coding (call function / return values)

查看:98
本文介绍了Python编码(调用函数/返回值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写此问题的代码时遇到麻烦.我已经在几个地方看到了这个问题,但是我仍然无法从他们提供的提示中找出答案.

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided.

问题是:编写一个具有两个功能的程序:first()和second().函数first()应打印字符串"In function first()",然后调用函数second().函数second()应打印字符串在函数second()中.在全局scopre中,应调用函数first().

The question is: Write a program that has two functions: first() and second(). Function first() should print the string "In function first()" and then call function second(). Function second() should print the string "In function second(). In the global scopre, you should call function first().

这是我的代码.

def first():
    first = "In function first"


def second():
    second = "In function second"

print first(), second()

这看起来更近了吗?仍然无法正常工作,但我重新启用了打印功能.

Does this look any closer? It still doesn't work but I put the print functions back in..

推荐答案

first =首先执行函数" 不会使函数返回首先执行函数"

我能理解您为什么会这样认为.在某些语言中,返回值的方法是将其分配给函数.在其他语言中,赋值是一个表达式,而在函数内部求值的最后一个表达式是返回值.因此,有许多种语言可以使您的工作正常进行.

I can understand why you might think otherwise. In some languages, the way to return a value is to assign it to the function. In other languages, assignment is an expression, and the last expression evaluated inside a function is the return value. So, there are a lot of languages where what you're doing would work.

但是Python并不是其中之一.在Python中,返回值的唯一方法是使用 return 语句.而且,如果您不使用 return 语句,则调用方只会得到 None .

But Python isn't one of them. In Python, the only way to return a value is with the return statement. And if you don't use a return statement, the caller just gets None.

因此,您的代码仅创建了一个与该函数同名的本地变量(令人困惑),为该变量分配了一个值,然后忽略了该变量,因此最终无效.

So, your code just creates a local variable with (confusingly) the same name as the function, assigns a value to that, and then ignores that variable, so it ultimately has no effect.

您想要的是:

def first():
    return "In function first"

def second():
    return "In function section"

print first(), second()


但是,尽管这将为您提供所需的输出,但实际上并没有按照您说的去做:


However, while that will give you the desired output, it isn't actually doing what you said you wanted:

function first()应该打印字符串"In function first()"

Function first() should print the string "In function first()"

在这种情况下,它应该具有 print ,而不是 return 其他人必须打印的值.

In that case, it should have a print in it, not return a value that someone else has to print.

…,然后调用函数second().

… and then call function second().

然后,您需要将对 second()的调用放在 first 的定义内,而不是在其外部.

Then you need to put the call to second() inside the definition of first, not outside of it.

second()函数应打印字符串"in second()函数.

Function second() should print the string "In function second().

同样,它应该打印,而不是 return .

And again, it should print, not return.

因此,这与您尝试执行的操作更好地匹配:

So, this is a much better match to what you were trying to do:

def first():
    print "In function first"
    second()

def second():
    print "In function second"

first()

话虽这么说,但您编写(使用此修复程序)的代码实际上比您想编写的代码看起来像更好的代码.从内部函数返回值并以最高级别处理输出通常比在各处放置 print 更为灵活.

That being said, the code you wrote (with this fix) actually seems like better code than the code you wanted to write. Returning values from inner functions and handling output at the highest level is generally more flexible than having prints all over the place.

这篇关于Python编码(调用函数/返回值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆