首页 > 解决方案 > 在使用 python 库 random_word 制作这个随机密码生成器时,elif 语句中的“随机词”有时会返回“无”

问题描述

import numpy as np
from random_word import RandomWords
r = RandomWords()

promptInput = str(input("Do you want to enter your on prompt or randomly generate it, answer 
in yes or no : "))

if promptInput == "yes" :
    passwordHint = str(input("Enter a prompt for making the password : "))
    nums = np.random.randint(1000, 9999)
    newHint = passwordHint.capitalize()
    nums = str(nums)

    password = newHint + nums
    print(password)

elif promptInput == "no" :
    randomWord = str(r.get_random_word(minLength = 5,maxLength = 7))
    nums = np.random.randint(1000, 9999)
    newRandomWord = randomWord.capitalize() 
    nums = str(nums)

    password2 = newRandomWord + nums
    print(password2)                       

打印这主要是返回“无”,然后是随机数,例如 - None8797

else :
    print("Enter yes or no only")

这也可能是一个问题,因为我之前想测试代码是否可以工作,所以我在 elif 语句中输入了 print("none")

标签: python-3.xif-statementrandom

解决方案


看起来你正在使用这个模块:

https://pypi.org/project/Random-Word/

如果您查看它的源代码,您会注意到它从 api.wordnik.com 的在线服务中获取随机单词。如果您尝试生成安全密码,这绝对是一个糟糕的主意。

更糟糕的是,该服务仅在提供有效 API 密钥时才会响应。该模块具有三个嵌入式 API 密钥:

---
API_KEY: 
 - "d146825 .......... (redacted)  .......... 92d3230"
 - "1eirq2g .......... (redacted)  .......... w02zyj7"
 - "c23b746 .......... (redacted)  .......... b9b658e"

我尝试使用其中之一,并得到以下响应:

{"message":"API rate limit exceeded"}

这可能是导致空响应的原因。我建议您立即停止使用此模块并找到更好的解决方案。


推荐阅读