格式化字符串和函数调用我有一个功能,displayHand(),它看起来像这样:打印在python
def displayHand(hand):
"""
Displays the letters currently in the hand.
For example:
>>> displayHand({'a':1, 'x':2, 'l':3, 'e':1})
Should print out something like:
a x x l l l e
The order of the letters is unimportant.
hand: dictionary (string -> int)
"""
for letter in hand.keys():
for j in range(hand[letter]):
print letter, # print all on the same line
print # print an empty line
它是为我提供的(从600X类)。如上所述,它需要一个字符串 - >整数的字典并打印出“手”。
我遇到越来越这显示正确的烦恼什么。当我尝试
print('Current Hand: ', displayHand(hand))
这就是我得到:
a d d m o q y
('Current Hand: ', None)
,因为这是由教师提供的,我敢肯定有代码是这样写的一个原因,有什么东西我米没有得到。
我希望得到的是输出这样的:
Current Hand: a d d m o q y
我绝对全新这个东西,所以我甚至不知道问什么问题。
我的评价:至于我能拼凑,displayHand()不返回任何东西,这是什么拧紧了这一切。但是,我如何捕获这个函数的打印输出并按照我想要的方式呈现它?我想我应该试图捕获它在一个字符串,并有返回,但假设
教师试图证明什么,我会怎么做,而不改变displayHand()方法?
如果我的评估是关闭的,发生了什么事?
编辑:此功能是给我的班级,我将不得不使用它是这样。我知道改变它返回一个str会容易得多,但是我怎么才能在没有这样做的情况下完成正确的输出?
进一步编辑:我处理,要求完全按照我写的输出autograder。对不起,如此挑剔,我很欣赏答案,如果不是这样的话,会使用它们。
最后编辑:感谢您的澄清 - 我会用你的想法,使这个函数做什么,我需要做一个内部的辅助函数!
FINAL final编辑:我想通了!我所要做的就是,
print('Current Hand:'),
displayHand(hand)
对于真正的最终编辑:
Hah! you got it too! thank you so much for the help, I appreciate it!
我想你的功能应该不是返回打印它们的值。 – Oli 2013-03-08 17:02:03