Python彩色控制台版

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python彩色控制台版

lanxiaofang​​​​​​​   2022-09-28 我要评论

名片管理系统

一、思路

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们