2017-05-31 74 views
0

我应该编写一个程序,它不断地向用户提供一个文件名,直到它输入正确的文件名为止。然后,find_min_percent应该接受一个参数,GDP.txt文件中的一行(str)然后遍历该行以找到最小值并返回该值。这里是我的代码到目前为止Python程序:读取文件

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)) 

我的问题是与readline()。它说变量文件是未定义的。此代码的大纲不包含“def find_min_percent(line):”部分中的文件。所以我不知道如何去解决这个问题。我也不能在函数之外设置行,因为我必须在程序的其他函数中使用相同的行变量来读取其他行。所以我不知道该怎么做才能保持不变

+1

为什么不保存'open_file()'的返回值并将它传递给'find_min_percent'? 'print(open_file())'调用'open_file()',打印返回的文件对象的表示形式,然后抛弃它。 –

+1

Oups!从一个错误处理程序递归是一个非常糟糕的主意......请从'open_file'中删除'break'行(在'return'之后无用),并从结尾删除糟糕的'open_file'行功能。 –

回答

2

您在一个函数中定义的变量不能从另一个函数访问。为了解决这个问题,你可以举例来说,做到这一点(存储在一个“主要功能”变量返回的值,并把它传递给你的下一个函数):

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)) 

顺便说一句,你用你的line变量的方式很奇怪。它仅在find_min_percent内部使用,但在函数的外部定义,甚至作为参数传递。为什么?你想达到什么目的?

(参见here用于关于功能之外定义变量的访问邮政)

+0

就像我在帖子结尾所说的那样,我应该写的实际程序比这个要长得多。我一块一块地写代码,这是迄今为止我得到的。我将它定义在函数之外的原因是因为稍后我需要一个find_max_percent和find_gdp来接收行字符串。 – Nora

+1

这不一定是个问题。你可以有一个“全局”变量'f'或'file',你可以访问模块中的任何地方(只要它是在函数之外定义的)。我会编辑我的答案,以便您可以看到。 –

+0

@NouraAsrar:如果你想要2个函数共享一个变量,最好的方法是传递一个像Thomas这样的参数。只能在特殊用例中使用的另一种方法是拥有一个全局变量。但是如果你不能在你的函数中声明它是全局的,'open_file'中的'file'将是隐藏全局变量的本地变量!如果你不明白这句话远离全局变量... –

0

返回file变量超出该函数的范围 固定编码的:

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)) 
0

的另一种方式这样做:

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()) 

请注意,我不是sur e关于您的find_min_percent方法的正确性。另外,如果您手动打开文件(不使用with open),则还需要明确关闭。