繁体   English   中英

Python脚本陷入无限循环

[英]Python script stuck in infinite loop

我正在为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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM