我在任何地方都找不到问题的解决方案,所以也许我可以在这里得到答案。基本上,我尝试用另一个函数打印一个元组,然后尝试将该输出包装在另一个函数中。如果我将文本包装在wrapped_string_linesplit()函数中,它可以完美工作,但我基本上不希望这样。也许有人可以解释我的错误在哪里,以及从另一个函数包装它的一个好的解决方案是什么。提前致谢
工作解决方案:
def wrapped_string_linesplit(arg):
print('#' * 30)
for item in arg:
print(item)
print('#' * 30)
def welcome_message():
first = 'Welcome to xyz'.center(30, ' ')
second = ('#' * 30).center(30, ' ')
third = 'New Game'.center(30, ' ')
fourth = 'Load Game'.center(30, ' ')
fifth = 'About'.center(30, ' ')
sixth = 'Quit Game'.center(30, ' ')
return first, second, third, fourth, fifth, sixth
wrapped_string_linesplit(welcome_message())
输出:
##############################
Welcome to xyz
##############################
New Game
Load Game
About
Quit Game
##############################
然后将代码更改为以下内容,根本不会打印换行而不会出现错误:
def message_wrapper(foo):
def wrap():
print('#' * 30)
foo()
print('#' * 30)
return wrap
def string_linesplit(arg):
for item in arg:
print(item)
message_wrapper(string_linesplit(welcome_message()))
输出:
Welcome to xyz
##############################
New Game
Load Game
About
Quit Game
##############################
下一个我完全不明白,这个抛出错误
foo()
TypeError:在 message_wrapper() 内调用 foo() 时,“NoneType”对象不可调用。为什么它必须有一个返回值才能从另一个函数调用?
def message_wrapper(foo):
print('#' * 30)
foo()
print('#' * 30)
def string_linesplit(arg):
for item in arg:
print(item)
message_wrapper(string_linesplit(welcome_message()))
至尊宝的传说
相关分类