简体   繁体   English

为什么在 Python 中打印函数的返回值?

[英]Why the return value of functions being printed in Python?

Very new to python, trying this -对 python 非常陌生,试试这个 -

def newlines():
    print()
    print()
    print()    
question = "Which online Course you have signed up, dude?"
response = "Good Luck to you, dude!"
print(question), newlines(), input(), newlines(), print(response)

In Python 3.2.* the output is this在 Python 3.2.* 中,输出是这样的

Which online Course you have signed up, dude?



Nothing



Good Luck to you, dude!

(None, None, "Nothing", None) # Where this output is coming from ?

Also this is not happening with python 3.3 beta这也不会发生在 python 3.3 beta

You must be at the interactive shell.您必须在交互式 shell 中。 When I run your code as a file, I get this output:当我将您的代码作为文件运行时,我得到以下输出:

$ python3.2 test.py
Which online Course you have signed up, dude?



dlkjdf



Good Luck to you, dude!
$

You only get your output at the console:您只能在控制台获得输出:

>>> def newlines():
...     print()
...     print()
...     print()    
... 
>>> question = "Which online Course you have signed up, dude?"
>>> response = "Good Luck to you, dude!"
>>> 
>>> print(question), newlines(), input(), newlines(), print(response)
Which online Course you have signed up, dude?



dljdldk



Good Luck to you, dude!
(None, None, 'dljdldk', None, None)
>>> 

This is because the console will print the representation of the last thing you typed in. The last statement is actually a tuple, so it gets printed at the end.这是因为控制台将打印您输入的最后一个内容的表示。最后一个语句实际上是一个元组,因此它会在最后打印。 Here's some examples:下面是一些例子:

>>> 3
3
>>> 4
4
>>> 3, 4, None, "hey"
(3, 4, None, 'hey')

When you write this:当你写这个:

print(question), newlines(), input(), newlines(), print(response)

It is actually a tuple, which holds the result of each function.它实际上是一个元组,它保存着每个函数的结果。

Simply breaking up the calls on individual lines will solve your problem.只需将各个线路上的呼叫分开即可解决您的问题。

print(question)
newlines()
input()
newlines()
print(response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM