8.15 课后作业
-
给定验证码长度n,生成随机验证码,验证码由数字、字母组成(参考chr()内置方法)
import random def yanzhengma(n): for i in range(n): s = random.randint(1, 3) if s == 1: print(chr(random.randint(48, 57)), end='') if s == 2: print(chr(random.randint(65, 90)), end='') if s == 3: print(chr(random.randint(97, 122)), end='') yanzhengma(10)
-
打印进度条
import time def line(n): start = time.perf_counter() for i in range(n + 1): # (f'\r{},end='')实现单行刷新字符串,依次覆盖前面的字符串 print(f'\r{((i / n) * 100): ^3.1f}%[{"#" * i}->{"." * (n - 1 - i)}] {time.perf_counter() - start:.2f}s',end='') time.sleep(0.1) line(int(input("输入一个整数:")))