尝试制作加密代码,但出现许多错误

时间:2020-02-06 03:45:02

标签: python python-3.x recursion encryption counter

alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ","."]
print(alphabet[0])
user_input = "this is a test."
user_sentance = list(user_input)

print(user_sentance)

#user_sentance = user_input.split()
#print(user_sentance)
counter = 0

#def remove_space():
 #   return string.replace(" ", "")

#def remove_period():
#    user_sentance.replace(".", "")

counter_1 = 0

def counter_2():
    counter_2 = 0   


def counter_1():
    counter_1 = 0   

def main():
    for i in user_sentance:
        counter_1 = 0
        counter_2 = 0
        for x in alphabet:

            if user_sentance[counter_1] == alphabet[counter_2]:
                print(counter_2)
                main()
                counter_2 = 0
            else:
                main()
                counter_2 += 1
        counter_1 += 1

        #if "." in user.sentance[counter]:
         #   user_sentance[counter].replace(".","")
          #  print(user_sentance)
           # main()
        #else:
        #    break


main()

这是打印内容:

a

['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', '.']

然后错误从这里开始:

Traceback (most recent call last):
  File "C:/Users/Shah family/Desktop/Coding/python/encrypted code.py", line 52, in <module>
    main()

  File "C:/Users/Shah family/Desktop/Coding/python/encrypted code.py", line 40, in main
    main()

  File "C:/Users/Shah family/Desktop/Coding/python/encrypted code.py", line 40, in main
    main()

  File "C:/Users/Shah family/Desktop/Coding/python/encrypted code.py", line 40, in main
    main()

 [Previous line repeated 989 more times]

 File "C:/Users/Shah family/Desktop/Coding/python/encrypted code.py", line 35, in main
    if user_sentance[counter_1] == alphabet[counter_2]:

RecursionError: maximum recursion depth exceeded in comparison

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您没有结束循环的条件。第38行上的main()不断被调用,由于该程序处于无限循环中,因此程序停止了。

您应该执行的操作,将if语句修复为具有结束或中断循环的条件,例如bool值,或者返回,或者检查此文章的评论。

递归地调用main函数绝不是一个好主意,这就是使其他函数起作用的原因。

相关问题