首页 > 解决方案 > 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 ?

标签: python

解决方案


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.


推荐阅读