Python脚本陷入无限循环

时间:2016-10-15 14:12:58

标签: python python-2.7

我正在为python挑战ARG编写一个初学者python脚本。基本目标是编写一个脚本,该脚本将读取大量文本,仅传递小写字符,其中任意一侧只有3个大写字母。该脚本应该从列表中查看传入的字符,将下3个字符和前3个字符存储在列表中。一旦发生这种情况,脚本将评估传入字符是否为大写,如果循环重新开始,则脚本会评估列表中的所有三个字符是否大写。如果满足条件,脚本应该打印当前字符,否则循环重新开始。

每当我运行此脚本时,我都没有关于代码的调试/错误/警告,但它永远不会完成或向文件写入任何内容。

这是我写的代码,任何帮助将不胜感激。

#code, where f = text to be processed, d = text file to be written to

f = open("test.txt", "r+")
f = f.read()
fList = list(f)
limit = len(fList)

#Set location of result
d = open("noided.txt", "r+")
i, j, k = [0, 0, 0]

#Main loop
#While there are characters left to be processed
while i < limit:
    #Skip the first 4 characters
    if i < 4:
        #print i, fList[i]
        i += 1
    else:
        #print i, fList[i]
        currentChar = fList[i]
        count = 0
        prevChars = [fList[i-1],fList[i-2],fList[i-3]]
        nextChars = fList[i:i + 3]

        if currentChar.isupper():

            i += 1

        else:
            while k < 3:
                if prevChars[k].isupper() and nextChars[k].isupper():
                    count += 1
                    k += 1
                elif count == 3:
                    print currentChar
                    d.write(currentChar)
                    i += 1
                else:
                    i += 1

1 个答案:

答案 0 :(得分:0)

对于初学者,您没有名为limit的变量。其次,使用堆栈类型的列表会更容易。把堆栈想象成一堆文件。您可以将文件中的字母解析为堆栈(使用for循环),但是您可以在每个字符进入堆栈之前对其进行比较,使此模块对此类项目有用

相关问题