代码随机不遵循For Loop的说明

我试图创建一个使用阶乘查找可能的配置数量的程序。到目前为止,这是我的代码:

from collections import Counter
letters = []
lettnum = []
trash = []
booleans = []
boolet = {}
def fact(n):
    if n == 0:
        return 1
    else:
        return n * fact(n - 1)

word = input("Word: ")
x = fact(len(word))
for char in word:
    letters.append(char)

z = Counter(letters)
y = list(z.values())
ans = "print("
print(letters)
print(y)

for element in y:
    if y[0] == element:
        ans = ans + str(fact(element))
    else:
        ans = ans + "*" + str(fact(element))
ans = ans + ")"
print(ans)

If you enter the word tool, it is supposed to give an answer of

print(1*2*1)

但是打印出来

print(1*1*21*1)

I've noticed that it combines the 2 and 1 without adding a *, and it also adds random 1's for absolutely no reason. What's up with my code?

更多信息:

  • 文本编辑器:Visual Studio Code
  • Python版本:Python 3.7.3