让我们说我定义一个简单的函数,它将显示传递给它的整数:
def funct1(param1):
print(param1)
return(param1)
Run Code Online (Sandbox Code Playgroud)
输出将是相同的,但我知道当return在函数中使用语句时,可以再次使用输出.否则,print不能使用语句的值.但我知道这不是正式的定义,任何人都能为我提供一个好的定义吗?
在python中我似乎没有理解返回函数.为什么在我可以打印时使用它?
def maximum(x, y):
if x > y:
print(x)
elif x == y:
print('The numbers are equal')
else:
print(y)
maximum(2, 3)
Run Code Online (Sandbox Code Playgroud)
这段代码给了我3.但是使用return它会做同样的事情.
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
Run Code Online (Sandbox Code Playgroud)
那两者之间的区别是什么?对不起,这个巨大的菜鸟问题!
直到最近这段代码才有效.我添加了一个函数来提示用户输入文件名和扩展名,然后这不起作用,所以我使用了这个版本.现在,当我尝试运行它时,我得到了这个:
Traceback (most recent call last):
File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 53, in <module>
main()
File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 51, in main
writer(final_counts(intermediate_count))
File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 31, in final_counts
for file_path in intermediate_file_list:
TypeError: 'function' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我也不完全确定错误意味着什么,并且在研究之后我只能在函数中发现python对象不可迭代错误和python3 TypeError:'function'对象不可迭代但不能解决我的问题.
下面是给我错误的代码:
def final_counts(intermediate_file_list):
date_list = {}
for file_path in intermediate_file_list:
with open(file_path, "r") as f:
for line in f:
tockens = line.split(",")
if tockens[0] in date_list:
date_list[tockens[0]] = date_list[tockens[0]] + …Run Code Online (Sandbox Code Playgroud)