2015-02-24 92 views
-5

我正在創建一個程序來匹配特定的一年到奧林匹克位置。缺少位置參數? (python幫助)

I.e.如果用戶輸入一年,它會找到當年奧運會發生的地點(1904年 - 希臘雅典..)等。

它一直告訴我我在我的代碼中有位置錯誤,但是:

Traceback (most recent call last): 
File "<pyshell#29>", line 1, in <module> findLocation() 
    TypeError: findLocation() missing 3 required positional arguments: 
    'yearList', 'locList', and 'year' 

這裏是我的程序:

def getData(): 

    print("All the events") 
    print("") 

    yearList = [] 
    locList = [] 

    readFile = open('olympics.txt', 'r') 

    for line in readFile: 
     year, loc = line.split("\t") 
     loc = loc.replace("\n", "") 
     yearList.append(year) 
     locList.append(loc) 

    return yearList,locList 


def findLocation(yearList, locList, year): 
    i=0 
    location="" 

    while i<len(locList): 
     if yearList[i] == year: 
      year = yearList[i] 
     elif yearList[i] !=year: 
      return print ("That was incorrect") 

     i += 1 

    return location 

獲取數據成功地工作,但我的findLocation功能似乎是不正常的,我不知道如何解決它。

這是包含奧運賽事的文本文件的摘錄。

1896 Athens, Greece 
1900 Paris, France 
1904 St. Louis, Missouri USA 
1906 Athens, Greece* 
1908 London, England 
1912 Stockholm, Sweden 
1916 Not held** 
1920 Antwerp, Belgium 

有人可以幫我嗎?

+0

'我的findLocation函數似乎沒有重擊'沒有什麼幫助:( – thefourtheye 2015-02-24 07:02:38

+0

你是什麼意思位置錯誤?你能提供錯誤信息(stacktrace)嗎? – 2015-02-24 07:07:30

+0

回溯(最近通話最後一個): 文件 「」,1號線,在 findLocation() 類型錯誤:findLocation()失蹤3所需的位置參數: 'yearList', 'locList' 和 '年' – 2015-02-24 07:08:17

回答

0

原因findLocation不會產生一個結果是因爲你遍歷所有的年/地區,如果第一個是不正確你從函數返回回報打印(「這是不正確」)

UPDATE:包括描述如何調用該函數

像這樣的東西應該更好地工作一例的主要方法:

def getData(): 
    print("All the events") 
    year_to_location = {} 
    with open('olympics.txt', 'r') as f: 
     content = f.readlines() 
     for line in content: 
      year, loc = line.split("\t") 
      year_to_location[year] = loc.strip() 
    return year_to_location 

def findLocation(year_to_location, year): 
    if year in year_to_location: 
     return year_to_location[year] 
    else: 
     print("That was incorrect") 

if __name__ == '__main__': 
    year_to_location = getData() 
    print(findLocation(year_to_location, "1900")) 

注意:我用year_to_location替換了year_list和loc_list,而是簡化了findLocation。我還添加了一個開放的(「olympics.txt」)爲f聲明這是一個文件處理稍微更好的方法(它確保完成後的文件處理程序被關閉)

你也剛從刪除回報返回打印(「這是不正確的」)和您的代碼應該按原樣工作。

0

but my findLocation function seems to be out of whack

你是對的。你的邏輯需要在那裏改變。但是,下次請添加更多關於您的預期和獲得的信息。這將幫助我們。現在的邏輯:

def findLocation(yearList, locList, year): 
    i=0 
    location="" 

    while i<len(locList): 
     if yearList[i] == year: 
      return locList[i] 
     elif int(yearList[i]) > int(year): 
      print "That was incorrect" 
      return 

     i += 1 
    print "Year was incorrect" 
    return 

現在這個函數需要用三個參數來調用。二是從您的GetData,一個來自用戶打印位置:

year_list,loc_list =的getData() 年=的raw_input( 「進入今年搜索」) findLocation(year_list,loc_list,年)

0

話雖如此,你的代碼是非慣用的並且它可以用更短,更清晰的方式重寫,讓我們試着分析你的問題。

你的錯誤是由函數簽名和您的使用情況之間存在不匹配的動機,但即使它被稱爲正常,將不會返回正確的location ...這是你的代碼

def findLocation(yearList, locList, year): 
    i=0 
    location="" 

在線之上設置location,它永遠不會重新分配,所以你要返回空字符串,irrispective你的代碼的其餘部分的

while i<len(locList): 
     if yearList[i] == year: 
      year = yearList[i] 

上面的測試是同義反復,如果兩個對象是相等的(即,他們的值相等),你一個人的價值分配給其他對象......你沒有改變任何東西

 elif yearList[i] !=year: 
      return print ("That was incorrect") 

上面的測試是不是你想要的,因爲1.你不想從你的函數退出,直到您已經測試了所有可能的Olympyc年,並且2.return print(...)返回由print函數返回的值,ir,None

 i += 1 
    return location 

力求貼近您的編碼風格,我會去這樣

def find_location(y_list, l_list, year): 
    i = 0 
    while i<len(y_list): 
     if year == y_list[i]: 
      # the following statement exits the function, 
      # returning the Olympics location for the year 
      return l_list[i] 
     i = i+1 
    return '' 

如果你瞭解zip內置的,下面是更緊湊,更表現

def f(y_l, l_l, year): 
     for y, l in zip(y_l, l_l): 
      if year==y: return l 
     return "" 

您問題的真正解決方案是使用不同的數據結構而不是配對列表s,即一個dict,但在我看來,你並沒有介紹到它...

相關問題