【发布时间】:2020-02-06 03:45:02
【问题描述】:
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
我该怎么办???
【问题讨论】:
-
您应该停止从
main()拨打main()。 -
您使用的递归函数没有停止/中断条件。
-
你应该:删除所有注释掉的代码;删除无意义的
counter_1和counter_2函数;在没有合适的停止条件的情况下,不要从main()调用main()此递归 - 或者可能更好,将递归放在定义明确的递归函数中并从main()调用一次。 -
程序应该做什么?如果我们不知道您想要的结果,包括您是否打算使用递归,我们无法推荐解决方案。
标签: python python-3.x recursion encryption counter