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()