2014-10-20 31 views
1

剛剛寫了一個python程序來確定助記符「I before E before except C after」的助記符是多麼有用。我在E程序不工作之前

隨着輸入:

'I before e except when conducting an efficient heist on eight foreign neighbors. I believe my friend has left the receipt for the diet books hidden in the ceiling' 

它會顯示:

Number of times the rule helped: 5 
Number of times the rule was broken: 5 

改變了一些事情,我想我改回來,但現在的代碼被打破,任何意見將有助於

while True: 

    line = input("Line: ")       
    count = 0          
    h = 0 
    nh = 0 


    words = line.split()        
    for x in range(0, len(words)):     
     word = words[count] 

     if "ie" in word:        
      if "cie" in word:      
       nh += 1 
      else: 
       h +=1 

     if "ei" in word: 
      if "cei" in word: 
       h += 1 
      else: 
       nh += 1 
     else: 
      h += 0 
    count += 1          

    print("Number of times the rule helped:",h)  
    print("Number of times the rule was broken:",nh) 
    print() 
+1

也許x應該做索引,不算? – user3125280 2014-10-20 02:30:58

+0

歡迎來到Stackoverflow。我們不是在這裏傾訴你的整個計劃,並猜測問題可能是什麼。請提供具體信息,說明代碼爲什麼「破碎」。有沒有一個測試案例沒有解決?如果是這樣,給我們所有的細節。 – 2014-10-20 02:30:59

+0

抱歉沒有提供足夠的信息,我在這裏仍然很新。發生了什麼事情是我之前發佈的輸入沒有規則的幫助或破壞,從返回答案的意義上來說沒有任何迴應。 但是,它仍檢測較小輸入中的規則破壞/跟蹤。我不知道爲什麼或者如何,但我認爲我已經打破了for循環或邏輯 – user3699151 2014-10-20 03:07:31

回答

0

你會很好地表達你的測試用例。我不完全明白你想要做什麼。

看起來你所需要的只是查看文本中出現了多少次'cie'或'cei'。

在這種情況下:

for i in range(0, len(line)): 

    print("scanning {0}".format(line[i:i+3])) 
    if line[i:i+3].lower() == "cie": 
     nh += 1 
    if line[i:i+3].lower() == "cei": 
     h += 1 
+0

嗯,似乎沒有工作。好主意,但它似乎應該工作。我會看看它 – user3699151 2014-10-20 09:32:49

+0

對不起,但像「這似乎沒有工作」的評論是沒用的。除非你可以給出非常明確的錯誤反饋等,否則你不會在stackoverflow上找到很多幫助。祝你好運,你的調試工作。 – user3556757 2014-10-20 11:02:31

1

好神我是個白癡。我可能總共花了3個小時左右的時間試圖解決這個問題。

for x in range(0, len(words)):     
    word = words[count] 

    if "ie" in word:        
     if "cie" in word:      
      nh += 1 
     else: 
      h +=1 

    if "ei" in word: 
     if "cei" in word: 
      h += 1 
     else: 
      nh += 1 
    count += 1 

任何人都可以發現這與舊代碼的相應部分之間的區別?最後「計數+ = 1」只是縮短了一段時間。所有這些小時都浪費了......對不起,如果我在這裏浪費別人的時間:|

+1

呵呵,歡迎來到我們的世界:)認真地說,很好的工作堅持下去 - 很多人只是在類似的編碼直線時放棄。話雖如此,我認爲你甚至不需要'count'-只用'word = words [x]'。 – 2014-10-20 16:47:15