我之前E程序不工作

时间:2014-10-20 02:27:43

标签: if-statement for-loop python-3.x input

刚刚编写了一个python程序,以确定助记符“我在E之前除了C之后”是多么有用。

输入:

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

2 个答案:

答案 0 :(得分: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   

有人能发现这与旧代码的相应部分之间的区别吗?最后的'count + = 1'只是缩短了一个额外的时间。所有这些时间都浪费了......对不起,如果我浪费了其他人在这里的时间:|

答案 1 :(得分: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