一、模拟高铁售票系统
import prettytable as pt
def show_ticket(row_num):
# 创建一张表格
tb = pt.PrettyTable()
# 设置标题
tb.field_names = ['行号', '座位1', '座位2', '座位3', '座位4', '座位5']
# 遍历有票
for i in range(1, row_num+1):
lst = [f'第{i}行', '有票', '有票', '有票', '有票', '有票']
tb.add_row(lst)
print(tb)
def order_ticket(row_num, row, column):
tb = pt.PrettyTable()
tb.field_names = ['行号', '座位1', '座位2', '座位3', '座位4', '座位5']
for i in range(1, row_num+1):
if int(row) == i:
lst = [f'第{i}行', '有票', '有票', '有票', '有票', '有票']
lst[int(column)] = '已售'
tb.add_row(lst)
else:
lst = [f'第{i}行', '有票', '有票', '有票', '有票', '有票']
tb.add_row(lst)
print(tb)
if __name__ == '__main__':
row_num = 6
show_ticket(row_num)
choose_num = input('请输入您选择的座位:如4,3: ')
row, column = choose_num.split(',')
# 系列解包赋值
order_ticket(row_num, row, column)
![](https://i-blog.csdnimg.cn/direct/309b534084e2484e8acd713a666a078f.png)
二、推算日期
import datetime
def input_date():
inputdate = input('请输入开始日期(20281001)后按回车:')
datestr = inputdate[0:4]+'-'+inputdate[4:6]+'-'+inputdate[6:]
# 类型转换
dt = datetime.datetime.strptime(datestr, '%Y-%m-%d')
return dt
if __name__ == '__main__':
date = input_date()
in_num = eval(input('请输入间隔天数:'))
date = date+datetime.timedelta(days=in_num)
print('推算的日期是:', date)
![](https://i-blog.csdnimg.cn/direct/ff81616bff9146eab66029b47dce2719.png)
三、实现词云图
import jieba
from wordcloud import WordCloud
# 读取数据
with open('华为笔记本.txt', 'r', encoding = 'utf-8') as file:
s = file.read()
lst = jieba.lcut(s)
stopword = ['运行速度', '屏幕效果', '散热性能']
txt = ''.join(lst)
# 绘制词云图
wordcloud = WordCloud(background_color='white', font_path='msyh.ttc', stopwords=stopword, width=800, height=600)
# 由txt生成词云图
wordcloud.generate(txt)
# 保存图片
wordcloud.to_file('华为笔记本评价词云图.png')
![](https://i-blog.csdnimg.cn/direct/c29041571442411f90f7e28e7fede6d2.png)