2017-05-31 75 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),則還需要明確關閉。