2012-08-04 147 views
-3

很新的蟒蛇,嘗試這一點 -爲什麼在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) 

在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 ? 

而且這是不是與蟒蛇3.3測試版

回答

5

您必須在交互式shell中。當我運行你的代碼作爲一個文件,我得到這樣的輸出:

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



dlkjdf 



Good Luck to you, dude! 
$ 

你只有你在控制檯輸出:

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

這是因爲控制檯將打印的最後一件事表示你輸入了。最後一個語句實際上是一個元組,所以它在最後打印出來。這裏有一些例子:

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

發生的事情當你寫這個。 :

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

它實際上是一個元組,它包含每個函數的結果。

只需打破個別電話上的呼叫即可解決您的問題。

print(question) 
newlines() 
input() 
newlines() 
print(response) 
+2

+1你應該這樣寫,不管其他答案中的信息如何。 ','不是Python中的語句分隔符。如果你想按順序做幾件事,那麼按順序做幾件事。 – 2012-08-04 20:36:00