Python名片管理系统+猜拳小游戏案例实现彩(色控制台版)
作者:lanxiaofang??????? 时间:2021-10-26 03:52:44
名片管理系统
一、思路
1、定义名片操作选项
2、把增加的名片信息存储到字典中
3、所有名片信息存储到列表
4、对于误操作给出提示
二、用到的知识点
1、类的定义,用来设置控制台输出颜色
2、函数的定义,用来输出欢迎与选项
3、if elif else 对选择的选项做出判断
三、效果
四、代码
"""
* @software: PyCharm
* @Description: 名片管理系统
"""
class BColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def cardHead():
print(BColors.HEADER)
print('=======欢迎进入名片管理系统=======')
print('1.查看名片')
print('2.添加名片')
print('3.修改名片')
print('4.删除名片')
print('5.退出系统')
print(BColors.ENDC)
l = [] # 使用列表,进行数据的增删改查
while True:
cardHead()
choose = input('请选择: ') # input 输出都是字符串
print(BColors.OKBLUE)
if choose == '1':
i = 0
if len(l) == 0:
print('暂无名片')
else:
while i < len(l):
print('%s->姓名:%s | 年龄:%s | 身高:%s' % (i, l[i]['name'], l[i]['age'], l[i]['high']))
i += 1
elif choose == '2':
name = input('name: ').strip()
age = input('age: ').strip()
high = input('high: ').strip()
info = {'name': name, 'age': age, 'high': high}
l.append(info)
print('添加成功')
elif choose == '3':
revise = input('请选择要修改的名片的ID: ')
if int(revise) >= len(l):
print('该ID不存在')
else:
name1 = input('name: ')
age1 = input('age ')
high1 = input('high: ')
if name1:
l[int(revise)]['name'] = name1
if age1:
l[int(revise)]['age'] = age1
if high1:
l[int(revise)]['high'] = high1
print('修改成功')
elif choose == '4':
del1 = input('请选择要删除的名片: ')
if int(del1) >= 0 and int(del1) < len(l):
l.remove(l[int(del1)])
print('删除成功')
else:
print('该ID不存在')
elif choose == '5':
print('退出成功,欢迎使用本简易名片系统')
break
else:
print('输出错误,请重新输入')
print(BColors.ENDC)
猜拳小游戏
一、思路
1、控制台输入数字代表石头剪刀布,用随机数随机石头剪刀布
2、对比控制台输入和随机到的结果
3、设置输出颜色
4、记录胜利、平局、失败次数
5、输入不在设定范围内提示输入有误
6、退出游戏告知胜率
二、用到的知识点
1、类的定义,用来设定输出颜色
2、判断if elif else 的使用
3、在死循环中退出循环 break
4、随机函数 random
5、字符串相等 ==
6、and or
三、效果
四、代码
"""
* @software: PyCharm
* @Description: 猜拳小游戏
"""
import random
class BColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
lose = 0
win = 0
ping = 0
while True:
print(BColors.HEADER + '**************************欢迎来猜拳*******************' + BColors.ENDC)
print('1 剪刀 2 石头 3 布 4 退出游戏')
print(BColors.UNDERLINE + '赢:%s 平:%s 输:%s' % (win, ping, lose) + BColors.ENDC)
robot = random.choice(['剪刀', '布', '石头'])
h = input(BColors.BOLD + '请出: ' + BColors.ENDC)
if (h == '1' and robot == '布') or (h == '2' and robot == '剪刀') or (h == '3' and robot == '石头'):
win += 1
print(BColors.OKGREEN + '很高兴,你赢了' + BColors.ENDC)
elif (h == '1' and robot == '剪刀') or (h == '2' and robot == '石头') or (h == '3' and robot == '布'):
ping += 1
print(BColors.OKBLUE + '很高兴,平局' + BColors.ENDC)
elif (h == '1' and robot == '石头') or (h == '2' and robot == '布') or (h == '3' and robot == '剪刀'):
lose += 1
print(BColors.FAIL + '很高兴,它赢了' + BColors.ENDC)
elif h == '4':
print('已退出游戏,游戏结果如下:')
print(BColors.UNDERLINE + '赢:%s 平:%s 输:%s' % (win, ping, lose) + BColors.ENDC)
print('获胜率:', str(win * 100 / (win + ping + lose)) + '%')
break
else:
print(BColors.WARNING + '输入错误' + BColors.ENDC)
来源:https://blog.51cto.com/u_15668438/5575150
标签:Python,系统,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python网络爬虫基于selenium爬取斗鱼直播信息
2023-03-05 09:33:19
Pandas统计计数value_counts()的使用
2023-11-24 10:28:46
![](https://img.aspxhome.com/file/2023/4/78714_0s.png)
Python网络编程中urllib2模块的用法总结
2022-05-26 18:49:53
![](https://img.aspxhome.com/file/2023/7/107637_0s.png)
php的ajax框架xajax入门与试用介绍
2023-09-27 14:46:16
python基于itchat实现微信群消息同步机器人
2023-08-02 03:23:51
![](https://img.aspxhome.com/file/2023/3/60733_0s.jpg)
python匿名函数的使用方法解析
2022-05-08 13:00:25
如何用Python绘制棒棒糖图表
2021-05-02 06:26:33
![](https://img.aspxhome.com/file/2023/5/98945_0s.png)
使用Python编写爬虫的基本模块及框架使用指南
2021-08-21 17:45:19
Python个人博客程序开发实例用户验证功能
2023-07-31 14:24:36
![](https://img.aspxhome.com/file/2023/2/61192_0s.png)
浅谈Python 敏感词过滤的实现
2022-12-11 07:47:17
php动态函数调用方法
2023-11-15 00:18:30
如何使用Python对Excel表格进行拼接合并
2021-03-15 18:01:54
![](https://img.aspxhome.com/file/2023/9/76039_0s.png)
Python修改Excel数据的实例代码
2021-05-24 12:40:29
详解OpenCV实现特征提取的方法
2022-10-05 11:15:28
![](https://img.aspxhome.com/file/2023/1/76071_0s.jpg)
Python使用Keras OCR实现从图像中删除文本
2022-07-22 20:50:24
![](https://img.aspxhome.com/file/2023/9/80569_0s.jpg)
Python爬虫之正则表达式基本用法实例分析
2022-12-20 17:02:16
![](https://img.aspxhome.com/file/2023/1/75361_0s.png)
关于利用:first-letter实现首字下沉的一些看法
2010-04-20 17:19:00
![](https://img.aspxhome.com/file/UploadPic/20104/20/first-letter_no_float-38s.png)
分享整理的12条sql语句连同数据
2012-07-11 16:14:59
Python 中将二进制转换为整数的多种方法
2023-01-11 17:30:53
Mootools 1.2教程(5)——事件处理
2008-11-19 16:33:00