2011-09-06 179 views
-2

練習題:「給定的單詞列表和文本文件,拼寫檢查的文本文件和打印所有的(唯一的)的話,其不在單詞列表中找到 的 內容。 「拼寫檢查程序

我沒能解決問題,以便有人可以告訴我怎麼去和正確的答案應該是什麼?:

由於這一聲明沒有在我的Python控制檯解析...

我嘗試:


a=list[....,.....,....,whatever goes here,...] 

data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()   

#I'm aware that something is wrong here since I get an error when I use it.....when I just write blablabla.txt it says that it can't find the thing. Is this function only gonna work if I'm working off the online IVLE program where all those files are automatically linked to the console or how would I do things from python without logging into the online IVLE? 

    for words in data: 

     for words not in a 

      print words 

wrong = words not in a 

right = words in a 

print="wrong spelling:" + "properly splled words:" + right 

哦,yeh ...我確定我已經正確地縮進了所有東西,但是我不知道如何在這裏格式化我的問題,這樣它就不會像現在這樣出現。抱歉。

您認爲如何?

+2

爲什麼不先修復語法錯誤?你不知道如何?我無法看到你的問題是什麼。是Python語法還是問題本身? – jergason

+0

這是_homework_,還是隻是一個你想要解決的練習?我主要是因爲標籤問。 –

回答

0

我不知道你在嘗試迭代什麼,但爲什麼你不只是第一次迭代你的單詞(這些單詞在變量a中,我猜?),然後對於你迭代的每個單詞單詞列表並檢查該單詞是否在單詞列表中。

我不會粘貼代碼,因爲它對我來說似乎是功課(如果是的話,請添加家庭作業標籤)。

順便說一句open()的第一個參數應該是一個字符串。

0

真的很簡單。將這兩個列表轉換爲sets,然後取difference。應該像10行代碼。你只需要自己弄清楚語法;)通過讓我們爲你寫信,你不會學到任何東西。

2

這段代碼有很多錯誤 - 我將在下面標記它們中的一部分,但我強烈建議您閱讀Python控制流構造,比較運算符和內置數據類型。

a=list[....,.....,....,whatever goes here,...] 

data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()   
# The filename needs to be a string value - put "C:\..." in quotes! 

for words in data: 
# data is a string - iterating over it will give you one letter 
# per iteration, not one word 

    for words not in a 
    # aside from syntax (remember the colons!), remember what for means - it 
    # executes its body once for every item in a collection. "not in a" is not a 
    # collection of any kind! 

     print words 

wrong = words not in a 
# this does not say what you think it says - "not in" is an operator which 
# takes an arbitrary value on the left, and some collection on the right, 
# and returns a single boolean value 

right = words in a 
# same as the previous line 

print="wrong spelling:" + "properly splled words:" + right