西安做网站培训,企业网站快速排名,公司网站建设代理怎么做,百度指数 网站文章目录topic:函数任务1、统计字符串中出现指定字符的次数任务2、格式化输出商品的名称和单价topic:函数
任务1、统计字符串中出现指定字符的次数
def get_count(s,ch):count0for item in s:if ch.upper()item or ch.lower()item:count1return count
…
文章目录
topic:函数
任务1、统计字符串中出现指定字符的次数
任务2、格式化输出商品的名称和单价
topic:函数
任务1、统计字符串中出现指定字符的次数
defget_count(s,ch):count=0for item in s:if ch.upper()==item or ch.lower()==item:count+=1return count
if __name__ =='__main__':s=str(input('请输入内容'))#'xiaojiaxiaojiawoaini'ch=input('请输入要统计的字符:')count=get_count(s,ch)print(f'{ch}在{s}中出现的次数为:{count}')
任务2、格式化输出商品的名称和单价
defshow(lst):for item in lst:for i in item:print(i, end='\t\t')print()
lst=[['01','电风扇','美的',500],['02','洗衣机','TCL',1000],['03','微波炉','老板',400]]print('编号\t\t名称\t\t\t品牌\t\t单价')
show(lst)print('-------------格式化----------------------')print('编号\t\t\t名称\t\t\t品牌\t\t单价')for item in lst:item[0]='0000'+item[0]item[3]='¥{:.2f}'.format(item[3])
show(lst)