Nora
Nora

Reputation: 15

Python program: reading a file

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

Upvotes: 0

Views: 1445

Answers (3)

Nurjan
Nurjan

Reputation: 6053

Another way of doing this:

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')
            print('user_input: ', user_input)
            line = file.readline(9)
            file.close()
            return find_min_percent(line)
        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())

Note that I am not sure about the correctness of your find_min_percent method. Also if you manually open file (without using with open) you need to explicitly close as well.

Upvotes: 0

Thomas Kowalski
Thomas Kowalski

Reputation: 2184

A variable you define in one function can't be accessed from the other one. To fix that, you could for example, do this (store the returned value in a "main function" variable and pass it to your next function):

def find_min_percent(line): 
    '''Find the min percent change in the line; return the value and the index.'''
    percent_lst = []
    # You can use f from this function, as long as you don't modify it
    line = f.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 


f = open_file()
print(f)
print (find_min_percent(line))

By the way, the way you use your line variable is weird. It's only used inside find_min_percent but defined outside of the function and even passed as a parameter. Why? What are you trying to achieve?

(see here for a post about accessing variables defined outside a function)

Upvotes: 2

Vijayabhaskar J
Vijayabhaskar J

Reputation: 436

The returned file variable is out of the scope of the function Fixed code:

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,file): 
    '''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 

temp=open_file()
print(temp)
print (find_min_percent(line,temp))

Upvotes: 0

Related Questions