python - why python code doesnt work without print-functions
问题描述
I have a code like
file=open("cd.txt","r")
x=[]
f=[]
def simpsons(x,f):
for line in file:
y=line.split()
x.append(float(y[0]))
f.append(float(y[1]))
return(x,f)
print(simpsons(x,f))
the problem is when I dont write "print(simpsons(x,f))" it takes x and f as an empty array I didnt understand. Why print functions changes it ?
解决方案
If you don't use the print function, all you are doing is defining the function and the lists will stay empty. You must call the function and return x and f in order for you to see their values.
You are essentially giving the computer instructions on how to do something, but you aren't telling it to go through those steps.
推荐阅读
- c# - 如何将服务依赖注入到 IHostedService(计划作业)中?
- java - 我可以在流口水规则的 LHS 中使用 Java 8 Lambda 表达式吗
- typescript - 将 Firebase 管理应用程序传递给数据访问对象
- html - 如何使用 flex 将一行与两个 div 隔开
- javascript - POST请求上的Asp.net Web API模型绑定NULL
- android - 如何创建一个片段在另一个片段上向上滑动的过渡?
- encryption - 如果解密消息已经是一个验证过程,在哪个用例中我需要签名?
- nginx-reverse-proxy - 在 nginx 反向代理后面查询外部 API 的问题
- spring - 如何使用 Response 对象返回状态码?
- javascript - 尽管使用了 localStorage,但主题切换器不会通过刷新持续存在