我是一个python noob,我不知道我的代码有什么问题。每当我运行它时,它就打印出来"这是你的密码:"当它打印出生成的密码时,没有任何内容。
import random
strength = ['Weak', 'Medium', 'Strong']
charbank = ('1234567890qwertyuiopASDFGHJKLZXCVBNM')
chosenchars = ('')
choice = ('')
def inputfunction():
while True:
userchoice = input("Would you like your password to be: \n Weak \n Medium? \n Strong?\n")
if userchoice in strength:
choice = ''.join(userchoice)
break
print ('oops, that\'s not one of the options. Enter again...')
return choice
def strengththing():
if choice == ("Weak"):
Weak()
if choice == ("Medium"):
Medium()
if choice == ("Strong"):
Strong()
def Weak():
passlen = 5
chosenchars.join(random.sample(charbank, passlen))
def Medium():
passlen = 10
chosenchars.join(random.sample(charbank, passlen))
def Strong():
passlen = 15
chosenchars.join(random.sample(charbank, passlen))
inputfunction()
strengththing()
print ('this is your password: %s' %chosenchars)
任何帮助都会很棒。我不知道我哪里出错了。谢谢!
答案 0 :(得分:0)
你没有改变'选择'的价值。在While循环之后使用return语句。
我做了一些修改。
你去了:
#!/usr/bin/env python3
import random
strength = ['Weak', 'Medium', 'Strong']
charbank = ('1234567890qwertyuiopASDFGHJKLZXCVBNM')
chosenchars = ('')
choice = ('')
def inputfunction():
while True:
userchoice = input("Would you like your password to be: \n Weak \n Medium? \n Strong?\n")
if userchoice in strength:
choice = ''.join(userchoice)
break
else:
print ('oops, that\'s not one of the options. Enter again...')
return choice
def strengththing():
if choice == 'Weak':
return RandomPass(5)
if choice == 'Medium':
return RandomPass(10)
if choice == 'Strong':
return RandomPass(15)
def RandomPass(passlen):
myVar = ''.join(random.sample(charbank, passlen))
return myVar
choice = inputfunction()
chosenchars = strengththing()
print ('this is your password: %s' %chosenchars)