我的Python代码有什么问题?尝试制作密码生成器

时间:2018-07-30 11:19:08

标签: python

我是Python的新手,正在研究一个简单的密码生成器脚本。当我尝试运行它时,遇到多个错误代码。我究竟做错了什么?

Traceback (most recent call last):
  File ".../Password Generator Simple", line 23, in <module>
    password(strength)
  File ".../Password Generator Simple", line 23, in password
    new_password.append(characters[random.randint(1-70)])
TypeError: randint() missing 1 required positional argument: 'b'

代码如下:

import random

characters = ["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","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",'1','2','3','4','5','6','7','8','9','0',"!","@","#","$","%","&","*","(",")"]

strength = input("Do you want a weak, medium or strong password?: ").lower()

new_password = []

def password(strength):
    if strength == 'weak':
        while len(new_password) != 5:
            new_password.append(characters[random.randint(1-70)])

    elif strength == 'medium':
        while len(new_password) != 8:
            new_password.append(characters[random.randint(1-70)])

    elif strength == 'strong':
        while len(new_password) != 14:
            new_password.append(characters[random.randint(1-70)])


    return new_password

password(strength)

new_password = "".join(new_password)

print(new_password)

1 个答案:

答案 0 :(得分:2)

该错误是在randint中

import random

characters = ["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","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",'1','2','3','4','5','6','7','8','9','0',"!","@","#","$","%","&","*","(",")"]

strength = input("Do you want a weak, medium or strong password?: ").lower()

new_password = []

def password(strength):
    if strength == 'weak':
        while len(new_password) != 5:
            new_password.append(characters[random.randint(1, 70)])

    elif strength == 'medium':
        while len(new_password) != 8:
            new_password.append(characters[random.randint(1, 70)])

    elif strength == 'strong':
        while len(new_password) != 14:
            new_password.append(characters[random.randint(1, 70)])


    return new_password

password(strength)

new_password = "".join(new_password)

print(new_password)

现在它应该可以正常工作