for i inrange(1,10):for x inrange(1,i+1):print(f'{i}*{x}={i*x}',end='\t')print()
2.杨辉三角
list=[1]
a1 =input("请输入行数: ")for i inrange(int(a1)):print(list)list.append(0)list=[list[j]+list[j -1]for j inrange(i +2)]#注意j的取值和取值后每行数组的实现
3.用户注册登录系统的实现(含发送验证码功能)
import time
import random
import utils.login_tool as tool
definit():print('欢迎进入数加登录系统')print('1.注册2.登录3.查看用户信息')defget_all_users():#获取用户信息函数模块
dict2_users ={}withopen('data/users.txt','r', encoding='utf-8')as f:
content = f.read()
info = content.split('\n')
info.pop(-1)for user in info:
dict2_users[user.split(',')[0]]= user.split(',')[1]return dict2_users
defregister():#用户注册函数模块print('欢迎注册'.center(50,'-'))
user_name =input('请输入你的用户名:')
pwd =input('请输入你的密码: ')
user_email =input('请输入您的邮箱:')
yzm = tool.random_code()
tool.send_email(user_email,f'【数加科技】您的验证码是: {yzm}, 在60秒之内使用,请勿泄露给他人。')
yzm_code =input('请输入您的验证码:')if yzm_code !=str(yzm):print('注册失败')returnNone
f =open('data/users.txt','a', encoding='utf-8')
f.write(f'{user_name},{pwd}\n')
f.close()
time.sleep(1)print(f'{user_name}注册成功')deflogin():#用户登录函数模块
user_name =input('请输入你的用户名: ')
pwd =input('请输入您的密码:')
User_name = get_all_users()if user_name notin User_name:print('没有用户,请先注册')returnNoneif pwd != User_name.get(user_name):print('登录失败')returnNoneprint('登录成功')defshow_users():#展示用户信息函数模块withopen('data/users.txt','r', encoding='utf-8')as f:
User_name = get_all_users()for x in User_name.items():print(f'用户名:{x[0]},密码:{x[1]}')if __name__ =='__main__':#调用main()函数实现各个模块
init()
dict1 ={'1': register,'2': login,'3': show_users
}
flag =Truewhile flag:
choice =input('请输入你的选择: ')if choice notin dict1:print('没有您想要的选择,请重新输入')continue
fun = dict1.get(choice)
fun()
flag =False
4.用户电影订票系统的实现
import os
from datetime import datetime
import hashlib
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
USER_PATH = os.path.join(BASE_PATH,'ticketing_system')defbook(user_file_path):
camera =input('请输入预定的电影院:')
movie =input('请输入您预定的电影:')
tickets =input("请输入您预定的票数:")
book_time = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
info =f'{camera}|{movie}|{tickets}|{book_time}\n'withopen(user_file_path,'a', encoding='utf-8')as f:
f.write(info)
f.flush()print('订票成功')defhistory(user_file_path):if os.path.exists(user_file_path):withopen(user_file_path,'r', encoding='utf-8')as f:
info = f.read()print(info)else:print('您没有历史订单')defrun():ifnot os.path.exists(USER_PATH):
os.makedirs(USER_PATH)
username =input('请输入您的用户名: ')
pwd =input('请输入您的密码: ')
s1 = hashlib.md5()
s1.update(pwd.encode('utf-8'))
res1 = s1.hexdigest()withopen('data/user.txt','a', encoding='utf-8')as f:
f.write(f'{username},{res1}\n')
f.flush()
f.close()
user_file_path = os.path.join(USER_PATH,f'{username}.txt')if os.path.exists(user_file_path):print(f'欢迎回来')else:print(f'您是新用户')
dict1 ={'1': book,'2': history
}whileTrue:
choice =input("请输入你的选择:")if choice.upper()=='Q':breakif choice =='1'or choice =='2':
fun = dict1.get(choice)
fun(user_file_path)else:print('没有您要的选择,请重新输入')continueif __name__ =='__main__':
run()
5.公司抽奖系统的实现
import random
list1 =[f'学生-小虎{i}'for i inrange(12)]
data_list =[('特等奖',1,'华为问界汽车一辆'),('一等奖',1,'10万元现金以及问界汽车10元优惠券'),('二等奖',3,'华为mate60 pro'),('三等奖',5,'华为手表')]defchoujiang(list1):#函数抽奖模块for grade,counts,lift in data_list[::-1]:print(f'开始抽{grade}奖'.center(50,'-'))
res_list = random.sample(list1,counts)for user in res_list:
list1.remove(user)
info =f'恭喜{",".join(res_list)}!! 每人获得{lift}奖品!!'yield info
res1 = choujiang(list1)whileTrue:
s1 =input('请按Y键进行抽奖')if s1 =='Y':try:
s2 = res1.__next__()print(s2)except:print('所有奖品全部抽完!!')breakelse:break