Python | 随机字符生成
import random
import string
import time
user_input = input("1.ascii_lowercase\n2.ascii_uppercase\n3.digits\n4.punctuation\n\nPlease choose (e.g., 123 for letters and digits): ")
type_choose = [int(char) for char in user_input if char in '1234']
try:
if not type_choose:
raise ValueError('Invalid choice')
except ValueError as e:
print(f'Value Error | {e}')
time.sleep(3)
exit()
#字符池
char_pool = []
if 1 in type_choose:
char_pool += string.ascii_lowercase
if 2 in type_choose:
char_pool += string.ascii_uppercase
if 3 in type_choose:
char_pool += string.digits
if 4 in type_choose:
char_pool += string.punctuation
#长度
try:
length = int(input('Char Length: '))
if length < 1:
raise ValueError('Length must be greater than 0')
except ValueError as e:
print(f'Value Error | {e}')
time.sleep(3)
exit()
#生成字符个数
try:
number = int(input('Number of chars to generate: '))
if number < 1:
raise ValueError('Number must be greater than 0')
except ValueError as e:
print(f'Value Error | {e}')
time.sleep(3)
exit()
i = 0
try:
while i < number:
char = "".join(random.choices(char_pool, k = length))
print(char)
f = open('chars.txt', 'a')
f.write(char + '\n')
i += 1
f.close()
print('chars.txt generated.\nExit in 30 seconds. Crl+C to exit now.')
time.sleep(30)
except ValueError:
print(f'Value Error | Unkown')
time.sleep(3)
exit()
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Steven
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果