Python Checker:TypeError:list indices必须是整数,而不是str

时间:2017-12-08 11:02:19

标签: python-3.x passwords

我真的很困惑为什么我继续得到这个错误,

password_strength['has_upper'] = False
TypeError: list indices must be integers, not str

,因为我一直在改变代码,但我仍然不明白。我正在尝试制作一个python密码检查器,我已经查看了所有其他问题,但没有人帮助过我。

代码是:

def Check():#menu function. Checks a password
global score
print ('Enter a password')
score = 0#sets the score to zero

while True:
    password = input('Password: ... ')
    if 7 <= len(password) < 24:
        break
    else:
        print ('The password must be between 8 and 24 characters.')

password_scores = {0:'Horrible', 5:'Weak', 10:'Medium', 15:'Strong'}#Choses the strength
password_strength = ['has_upper', 'has_lower', 'has_num']

if re.search(r'[A-Z]', password):
    password_strength['has_upper'] = True#checks if the password has a capital letter
    score=+5
else:
    password_strength['has_upper'] = False
score=-5#Taking away 5 points from the score

if re.search(r'[a-z]', password):
    password_strength['has_lower'] = True#checks if the password has a lower case letter
    score=+5
else:
    password_strength['has_lower'] = False
score= -5

if re.search(r'[0-9]', password):
    password_strength['has_num'] = True#Checking if the password has any numbers
    score=+5
else:
    password_strength['has_num'] = False
score=-5

score = len([b for b in password_strength.values() if b])#comparing the score against the strength

print ('Password is %s' % password_scores[score])#prints how strong the password is
print ("Score is: " + str(score))

请您提供任何建议或想法可能是什么问题,这将不胜感激。感谢信。

2 个答案:

答案 0 :(得分:0)

您似乎对列表感到困惑:

['value1','value2']

字典:

{'key1' : 'value1','key2' : 'value2'}

列表具有数字键(位置),字典可以具有字符串作为键。将password_strength初始化为字典,它将起作用:

password_strength = {'has_upper' : False, 'has_lower' : False, 'has_num': False}

答案 1 :(得分:0)

稍微更改了代码:

import re

def Check():  # menu function. Checks a password
    global score


print('Enter a password')
score = 0  # sets the score to zero

while True:
    password = input('Password: ... ')
    if 7 <= len(password) < 24:
        break
    else:
        print('The password must be between 8 and 24 characters.')

password_scores = {0: 'Horrible', 5: 'Weak', 10: 'Medium', 15: 'Strong'}  # Choses the strength
password_strength = {'has_upper':'0','has_lower':'0','has_num':'0'}

if re.search(r'[A-Z]', password):
    score = +5
    password_strength['has_upper'] = score  # checks if the password has a capital letter

else:
    score = -5  # Taking away 5 points from the score
    password_strength['has_upper'] = score

if re.search(r'[a-z]', password):
    score = +5
    password_strength['has_lower'] = score  # checks if the password has a lower case letter

else:
    score = -5
    password_strength['has_lower'] = score

if re.search(r'[0-9]', password):
    score = +5
    password_strength['has_num'] = score  # Checking if the password has any numbers

else:
    score = -5
    password_strength['has_num'] = score

score = sum(password_strength.values())  # comparing the score against the strength
print(score)

print('Password is %s' % password_scores[score])  # prints how strong the password is
print("Score is: " + str(score))