Python 将输出汇总到字典中
如何正确运行最后一行?有什么想法吗?首先,您需要确保函数实际返回一个值。将其从打印更改为返回。然后在字典中调用这些函数(注意函数名周围的括号?)Python 将输出汇总到字典中,python,Python,如何正确运行最后一行?有什么想法吗?首先,您需要确保函数实际返回一个值。将其从打印更改为返回。然后在字典中调用这些函数(注意函数名周围的括号?) 您应该返回函数中的值,如果您只是print(),它们的返回值将是None。如果您的目标是在firstone()的输出是键,而firsttwo()的输出是值时创建dict: 代码: def firstone(): x= 1 return x def firsttwo(): y=1 return y d = {"
您应该
返回函数中的值,如果您只是print()
,它们的返回值将是None
。如果您的目标是在firstone()
的输出是键,而firsttwo()
的输出是值时创建dict
:
代码:
def firstone():
x= 1
return x
def firsttwo():
y=1
return y
d = {"firstone":firstone(),"firsttwo":firsttwo()}
def firstone():
return 1
def firsttwo():
return 1
d = {firstone(): firsttwo()}
输出:
def firstone():
x= 1
return x
def firsttwo():
y=1
return y
d = {"firstone":firstone(),"firsttwo":firsttwo()}
def firstone():
return 1
def firsttwo():
return 1
d = {firstone(): firsttwo()}
另外,这两个函数都打印到屏幕上,并且不返回任何内容。这就是为什么你不能合并结果:没有。@Mark谢谢你的建议,我编辑了它。@576i因为线程中没有firsttwo()和firstone()代码,我想了解如何在字典中一起运行它们?