I'm supposed to code for a program that continuously asks a users for a file name until it enters the correct one. Then the find_min_percent is supposed to take one argument, one line (str) from the GDP.txt file then iterate through the line to find the smallest value and return that value. Here's my code so far
line = " "
def open_file():
''' Repeatedly prompt until a valid file name allows the file to be opened.'''
while True:
user_input = input('Enter a file name: ')
try:
file = open(user_input, 'r')
return file
break
except FileNotFoundError:
print('Error. Please try again')
open_file()
def find_min_percent(line):
'''Find the min percent change in the line; return the value and the index.'''
percent_lst = []
line = file.readline(9)
percent_lst += [line]
percent_int = [float(i) for i in percent_lst]
min_value = 10000
for percent in percent_int:
if percent < min_value:
min_value = percent
return min_value
print(open_file())
print (find_min_percent(line))
My problem is with the readline(). It says that the variable file is undefined. The outline of this code does not include file in the "def find_min_percent(line):" part. So I don't know how I would go about fixing this. I also can't set line outside of the function because I have to use the same line variable for other functions later in the program to read other lines. So I don't know what to do so that it doesn't keep