Python 我如何打印一些东西,然后调用一个打印在同一行上的函数?
在Python中,我有一个打印某些内容的函数。我想事先在同一行上打印一些东西 因此,我使用以下代码Python 我如何打印一些东西,然后调用一个打印在同一行上的函数?,python,printing,Python,Printing,在Python中,我有一个打印某些内容的函数。我想事先在同一行上打印一些东西 因此,我使用以下代码 print 'The hand is', displayHand(hand) def displayHand(hand): for letter in hand.keys(): for j in range(hand[letter]): print letter, # print all on the same lin
print 'The hand is', displayHand(hand)
def displayHand(hand):
for letter in hand.keys():
for j in range(hand[letter]):
print letter, # print all on the same line
print # print an empty line
但是,发生的情况是,函数内的打印由函数外的打印调用
如何打印开头字符串,然后调用我的函数?将
displayHand
重命名为renderHand
,并让它返回字符串。@zmbq提供的返回答案显然是正确的,但是如果您仍然想要使用更新的print
,假设您使用的是python>=2.6
from __future__ import print_function
print("Something", end="")
有了它,您可以不使用\n
打印。
所以基本上你可以这样做:
print("The hand is", end="")
displayHand(hand)
在函数中:
print("letter", end="")
对于2.x:
print 'The hand is',
displayHand(hand)
print
对于3.x:
print('The hand is', end="")
displayHand(hand)
print()
更好的方法是将“手是”的打印移到函数本身。请提供一个示例输入和预期输出