我正在尝试编写一个基本的密码检查程序,并且每当用户使用两次尝试输入正确的密码时,我都应该打印一个关于它们中有多少个字母的提示密码。 密码是可变的,我存储在正确的字符串。Python:计算字符串中有多少个字母
elif attempts == 1:
letters = 0
for i in password:
if password[i].isaplha():
letters = letters + 1
print('There are',letters,'letters in your password')
我的代码提供了错误
TypeErrorTraceback (most recent call last)
<ipython-input-195-c967c81bda5f> in <module>()
letters = 0
for i in password:
----> if password[i].isaplha():
letters = letters + 1
TypeError: string indices must be integers
我在做什么错?
为什么你觉得有什么不对呢?给一个[mcve]我们实际上可以测试一个输入,预期和实际输出。 – jonrsharpe
[字符串中的字母数]可能的重复(http://stackoverflow.com/questions/2932511/letter-count-on-a-string) – bc004346