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

python 3.3 beta也不会发生这种情况

4

2 回答 2

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')
于 2012-08-04T19:56:49.193 回答
2

当你写这个:

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

它实际上是一个元组,它保存了每个函数的结果。

简单地分解各个线路上的呼叫将解决您的问题。

print(question)
newlines()
input()
newlines()
print(response)
于 2012-08-04T19:54:15.943 回答