使用方法:把每个案例分别执行下,看它的运行结果,再试着根据括号提示编写程序代码
题目:
# ============ 1、设置字体颜色 ‘\033[x:xm xxxxxxxx\033[0m’
print('\033[0:35m这句话是紫红色字体\033[0m')
# ============ 2、把数字转为二、八、十六进制数
'''
def fun():
num = int(input('请输入一个十进制整数整数:'))
er = bin(num)
ba = oct(num)
shiliu = hex(num)
print(type(er)) # str
print(num,'的二进制表示为:',bin(num)) # 使用了个数可变的位置参数*args
print(str(num)+'的二进制表示为:'+bin(num)) # 字符串拼接
print('%d的二进制表示为:%s' % (num, bin(num))) # 格式化字符串
print('{0}的二进制表示为:{1}'.format(num,bin(num))) # 格式化字符串
print(f'{num}的二进制表示为:{bin(num)}') # 格式化字符串
if __name__ == '__main__': # 这个逻辑很nb,输入错了就反复执行,对了才跳出死循环
while True: # 死循环,所以得有break
try:
fun()
break
except:
print('程序出错,重新输入')
'''
# ============ 3、判断支付密码是否合法(只能是数字) .isdigit()
'''
pwd = input('请输入密码(只能是数字):')
if pwd.isdigit():
pass
else:
print('输入不合法')
# 法2:A if 判断 else B
print('' if pwd.isdigit() else '输入不合法')
'''
# ============ 4、循环输出26个字母对应ascii
'''
x = 97
for _ in range(1,27): # 只用到次数,用_占位
print(chr(x),'-->',x) # chr():转为字符
x+=1
print('----------------------------')
x = 97
while x < 97+26:
print(chr(x),'-->',x)
x+=1
'''
# ============ 5、模拟账户登录
'''
ci = 1
while ci<=3:
name = input('请输入用户名:')
mm = input('请输入密码:')
if name=='admin' and mm=='888':
print('登录成功')
break
else:
yu = 3-ci
if yu:
print(f'输入有误,还有{yu}次机会')
else:
print('登录失败')
ci+=1
'''
# ============ 6、猜数游戏:我心中有个1~100的数 random.randint
'''
import random
comp = random.randint(1,100)
print('电脑:从1~100猜个数')
ci = 1
while True:
me = int(input('我猜:'))
if mecomp:
print('大了')
ci+=1
else:
print('猜中了,所用次数为%d次' % ci)
break
'''
'''引子:在计算机中,00会当成0打印输出
x = 00
print(x) # 0
'''
# ============ 7、千年危机:根据年份后两位,补齐年份(4位)(列表) enumerate
'''
year = [82,98,75,0,99]
print(list(enumerate(year))) # [(0, 82), (1, 98), (2, 75), (3, 0), (4, 99)]
newyear = []
for index,value in enumerate(year): # enumerate:将对象组合为索引序列
print(index,value)
if value:
newyear.append(int('19'+str(value)))
else:
newyear.append(int('200'+str(value)))
print(newyear)
newyear.sort()
print(newyear)
'''
# ============ 8、进货 出货(列表) .find()
'''
goods = []
for item in range(0,5):
goods.append(input('请输入商品编号和名称:'))
print('当前货物有:')
for item in goods:
print(item) # 进货完成
car = []
while True:
num = input('请输入你想买的物品编号:')
for item in goods:
if item.find(num)!=-1: # .find()找不到时会返回-1
car.append(item)
print('已加入购物车,请继续添加')
break # 跳出for循环
if num == 'q':
break # 跳出while
print('购物车里有:')
# 逆序遍历购物车(因为先买的东西在下边显示)
for i in range(len(car)-1,-1,-1):
print(car[i]) # 出货完成
'''
# ============ 9、逆序遍历列表
'''
lst = ['0x','1st','2nd','3rd','4th']
for item in lst: # 顺序
print(item)
print('----------1,将列表逆置------------')
for item in lst[::-1]:
print(item)
print('----------2,通过内置函数------------')
for item in reversed(lst): #
print(item)
print('----------3,通过下标逆置------------')
for i in range(len(lst)-1,-1,-1): #
print(lst[i])
'''
# ============ 10、查询动漫分类(列表) zip dict.get()
'''
cartoon = ['四谎','巨人','月刊','小樱']
category = ['治愈','热血','搞笑','少女']
lst = zip(cartoon,category)
for item in lst:
print(item,end=' ')
print('\n')
d = dict(zip(cartoon,category))
for item in d:
print(item,d[item])
key = input('输入你喜欢的动漫,查看所属类型:')
print(key,'的类型是',d.get(key)) # 输入不存在时,get返回None
print(key+'的类型是'+d.get(key)) # 输入不存在时,报错
'''
# ============ 11、查询车次信息(字典+列表) 遍历
'''
dict_ticket={'G1569':['北京南-天津南','18:05','18:39','00:34'],
'G1567':['北京南-天津南','18:15','18:49','00:34'],
'G8917':['北京南-天津西','18:20','19:19','00:59'],
'G203':['北京南-天津南','18:35','19:09','00:34']}
print('车次\t\t出发站-到达站\t 出发时间\t 到达时间\t 历时时间')
for key in dict_ticket:
print('%-5s' % key,end=' ')
for item in dict_ticket[key]:
print(item,end=' ')
print() # 换行
num = input('请输入要购买的车次:')
persons = input('请输入乘车人,如果是多人,请用逗号隔开')
info_lst = dict_ticket[num]
stop = info_lst[0]
start = info_lst[1]
print(f'您已购买{num}次列车,{stop} {start}开,请{persons}尽快换取纸质车票')
'''
# ============ 12、点餐(元组) enumerate()
'''
print('本店咖啡有:')
coffee_tup = ('蓝山','雀巢','卡布奇诺','美式','猫屎','拿铁')
for index,item in enumerate(coffee_tup):
print(index+1,item,end=' ')
print()
num = int(input('请输入您选择的咖啡编号:'))
if 1 <= num <= len(coffee_tup):
print(f'您的{coffee_tup[num-1]}好了,请慢用')
'''
# ============ 13、打印榜单(元组) enumerate()
'''
post = (('广州恒大',72),('北京国安',70),('上海上港',66),('江苏苏宁',53),('山东鲁能',51),)
for index,item in enumerate(post):
print(index+1,'.',end=' ')
for i in item:
print(i,end=' ')
print()
'''
# ============ 14、模拟手机通讯录 (集合)
'''
phones = set() # 创建空集合
for i in range(5):
info = input(f'请输入第{i+1}个好友的姓名与号码:')
phones.add(info)
for item in phones:
print(item)
'''
# ============ 15、统计字符串出现个数(字符、字符串)
'''
str = input('请输入随意一串字符串:')
target = input('请输入要查找的字符:')
print('出现次数为:',end=' ')
ci = 0
for i in str:
if target.upper()==i or target.lower()==i:
ci+=1
print(ci)
'''
# ============ 16、字符串格式化输出 (字符串拼接)
'''
print('编号\t\t名称\t\t\t单价')
lst=[['01','电风扇','30'],
['02','洗衣机','1000'],
['03','手表 ','500'],]
for item in lst:
for i in item:
print(i,end='\t\t')
print()
for item in lst:
item[0] = '0000'+item[0]
item[2] = '¥'+item[2]+'.00'
for item in lst:
for i in item:
print(i,end='\t')
print()
'''
# ============ 17、能否构成三角形 (函数中异常处理) raise Exception
'''
def is_sanjiao(a,b,c):
if a<0 or b<0 or c<0:
raise Exception('三条边不能是负数')
if a+b>c and a+c>b and b+c>a:
print('ok')
else:
raise Exception('不能构成三角形')
if __name__ == '__main__':
try:
a=int(input('a:'))
b=int(input('b:'))
c=int(input('c:'))
is_sanjiao(a,b,c)
except Exception as e:
print(e)
'''