The below code block returns the data I want, however I have called 'read_from_file(filename) from outside the if statement, where filename doesn't exist.
I know I have two options to improve this:
- call read_from_file(filename) from inside my if statement, or
- make filename a global variable.
However when I attempt both I run into other issues such as the text from the file not returning. Would someone be able to show the correct way to do one of the above options?
filename = input('input the filename: ')
#read the file
def read_from_file(filename):
content = []
with open(filename, "r") as file:
for line in file:
line = line.strip()
if "-" not in line:
content.append(float(line))
return content
print(read_from_file(filename))