Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

Python Coding (call function / return values)

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.

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().

This is the code I have..

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

first = "In function first" doesn't make the function return "In function 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.

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.

What you want is:

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() should print the string "In function first()"

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

… and then call function second().

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

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

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()

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...