通过创建一个新功能来制作密码生成器代码,如果不是的话,想要使用


import random
import string

a = int(input("How may characters in letter format password"))
def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
print (random_char(a))

这工作正常,但是如果要给用户选择,假设如果我按一个它应该调用字母,如果我按 2,它应该以小写形式调用密码

任何人都可以帮助我或指导我或建议我

import random
import string

a = int(input("How may characters in letter format password"))
def random_char_l(y):
return ''.join(random.choice([x.lower() for x in string.ascii_letters]) 
def random_char_u(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
choice = int(input("1 for lower, 2 for upper: "))
if choice == 1:
print(random_char_l(a))
else:
print(random_char_u(a)

有点草率的实现,但它应该可以完成这项工作。

您可以使用菜单驱动的系统,如下所示,

  1. 生成

  2. 降低

    option = int(input("select menu option")

    if option == 1: call your generate function here elif option == 2: call the function, but this time use the .lower() method on your final string

这是你的意思吗?

你可以使它更加复杂和万无一失,但要回答你的具体问题,你可以创建另一个调用.lower()的函数,并有一些if语句功能。

import random
import string

a = int(input("How may characters in letter format password"))
b = int(input("Will the password be lowercase? Type 1 for Yes and 2 for No."))
def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))

def random_char_lower(y):
return ''.join(random.choice(string.ascii_letters).lower() for x in range(y))

if b == 1:
print (random_char(a))
elif b == 2:
print (random_char_lower(a))
else: print ("Invalid Selection") 

相关内容

最新更新