首页 > 其他分享 >实验3

实验3

时间:2023-04-25 13:23:24浏览次数:25  
标签:count pro cart 实验 print products id

import random
print('用列表存储随机整数: ')
lst = [random.randint(0, 100) for i in range(5)]
print(lst)
print('\n用集合存储随机整数: ')
s1 = {random.randint(0, 100) for i in range(5)}
print(s1)
print('\n用集合存储随机整数: ')
s2 = set()
while len(s2) < 5:
    s2.add(random.randint(0, 100))
print(s2)

  

1.random.randint(0,100)生成随机数整数的范围是[0,100],可以取到100。

2.list(range(5))生成的是[0, 1, 2, 3, 4],不包括5。

  list(range(1,5))生成的有序序列范围是[1, 2, 3, 4],不包括5.

3.不一定,随机数有可能重复,但只会出现一次。

4.一定是5。

lst = [55, 92, 88, 79, 96]

i = 0
while i < len(lst):
    print(lst[i], end = ' ')
    i += 1

print()

for i in range(len(lst)):
    print(lst[i], end = ' ')
print()

for i in lst:
    print(i , end = ' ')
print()

  

 

book_into = {'isbn': '987-7-5356-8297-0',
             '书名': '白鲸记',
             '作者': '克里斯多夫.夏布特',
             '译者': '高文婧',
             '出版社' : '湖南美术出版社',
             '售价': 82}
for key, value in book_into.items():
    print(f'{key}: {value}')
print()

  

book_infos = [{'书名':'昨日的世界','作者':'斯蒂芬.茨威格'},{'书名':'局外人','作者':'阿尔贝.加缪'},{'书名':'设计中的设计','作者':'原研哉'},{'书名':'万历十五年','作者':'黄仁宇'},{'书名':'刀锋','作者':'毛姆'}]
bookname = []
author = []
for i in range (len(book_infos)):
    bookname.append(book_infos[i]['书名'])
    author.append(book_infos[i]['作者'])
for i in range (0,5):
    print(f'{i+1}.《{bookname[i]}》,{author[i]}')

  

text = '''The Zen of Python, by Tim Peters
 
 Beautiful is better than ugly.
 Explicit is better than implicit.
 Simple is better than complex.
 Complex is better than complicated.
 Flat is better than nested.
 Sparse is better than dense.
 Readability counts.
 Special cases aren't special enough to break the rules.
 Although practicality beats purity.
 Errors should never pass silently.
 Unless explicitly silenced.
 In the face of ambiguity, refuse the temptation to guess.
 There should be one-- and preferably only one --obvious way to do it.
 Although that way may not be obvious at first unless you're Dutch.
 Now is better than never.
 Although never is often better than *right* now.
 If the implementation is hard to explain, it's a bad idea.
 If the implementation is easy to explain, it may be a good idea.
 Namespaces are one honking great idea -- let's do more of those!'''

list1 = list(text.lower().split())
d1 = dict.fromkeys('abcdefghijklmnopqrstuvwxyz',0)
for i in range (len(list1)):
    for j in list1[i]:
        if ord(j) in range(97,97+26):
            d1[j] += 1
        else:
            continue
d2 = sorted([(x[1],x[0]) for x in d1.items()],reverse = True)
d3 = [(x[1],x[0]) for x in d2]
for i in range (len(d3)):
    print(f'{d3[i][0]}:{d3[i][1]}')

  

code_majors={8326:'地信类',
8329:'计算机类',
8330:'气科类',
8336:'防灾工程',
8345:'海洋科学',
8382:'气象工程'}
print('{0:-^50}'.format('专业代号信息'))
for key,value in code_majors.items():
    print(f'{key}:{value}')
print('{0:-^50}'.format('学生专业查询'))
while True:
    x = input('请输入学号:')
    if x == '#':
        print('查询结束...')
        break
    else:
        y = eval(x[4:8])
        if y in list(code_majors.keys()):
            print(f'专业是:{code_majors[y]}')
        else:
            print('不在这些专业中...')

  

 

print("猜猜2020年哪一天会是你的lucky day:-")
import random
lucky_day=random.randint(1,31)
x=int(lucky_day)
x1=int(input("你有三次机会,猜吧(1~31):"))
if x1==x:
    print("哇,猜中了")
elif x1<x:
    print("猜早啦,你的lucky day还没到呢")
    x2=int(input("再猜(1~31):"))
    if x2==x:
       print("哇,猜中了")
    elif x2<x:
       print("猜早啦,你的lucky day还没到呢")
       x3=int(input("再猜(1~31):"))
       if x3==x:
           print("哇,猜中了")
       elif x3<x:
           print("猜早啦,你的lucky day还没到呢")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
       elif x3>x:
           print("猜晚啦,你的lucky day已经过了")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
    elif x2>x:
       print("猜晚啦,你的lucky day已经过了")
       x3=int(input("再猜(1~31):"))
       if x3==x:
           print("哇,猜中了")
       elif x3<x:
           print("猜早啦,你的lucky day还没到呢")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
       elif x3>x:
           print("猜晚啦,你的lucky day已经过了")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
elif x1>x:
   print("猜晚啦,你的lucky day已经过了")
   x2=int(input("再猜(1~31):"))
   if x2==x:
       print("哇,猜中了")
   elif x2<x:
       print("猜早啦,你的lucky day还没到呢")
       x3=int(input("再猜(1~31):"))
       if x3==x:
           print("哇,猜中了")
       elif x3<x:
           print("猜早啦,你的lucky day还没到呢")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
       elif x3>x:
           print("猜晚啦,你的lucky day已经过了")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
   elif x2>x:
       print("猜晚啦,你的lucky day已经过了")
       x3=int(input("再猜(1~31):"))
       if x3==x:
           print("哇,猜中了")
       elif x3<x:
           print("猜早啦,你的lucky day还没到呢")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
       elif x3>x:
           print("猜晚啦,你的lucky day已经过了")
           print("次数用光啦")
           print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")

  

datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
'2049777002': ['音乐', '旅行'],
'2049777003': ['马拉松', '健身', '游戏'],
'2049777004': [],
'2049777005': ['足球', '阅读'],
'2049777006': ['发呆', '闲逛'],
'2049777007': [],
'2049777008': ['书法', '电影'],
'2049777009': ['音乐', '阅读', '电影', '漫画'],
'2049777010': ['数学', '推理', '音乐', '旅行']}
v={}
for i in datas.values():
    for j in i:
        v[j]=v.get(j,0)+1
x=list(v.keys())
y=list(v.values())
z=[(y[i],x[i] ) for i in range(len(x))]
for j in sorted(z,reverse=True):
    print(f"{j[1]}:{j[0]}")

  

print('欢迎使用家用电器销售系统!')
pro=[['0001','电视机','海尔',5999.00,20],
          ['0002','冰箱','西门子',6998.00,15],
          ['0003','洗衣机','小天鹅',1999.00,10],
          ['0004','空调','格力',3900.00,0],
          ['0005','热水器','格力',688.00,30],
          ['0006','笔记本','联想',5699.00,10],
          ['0007','微波炉','苏泊尔',480.00,33],
          ['0008','投影仪','松下',1250.00,12],
          ['0009','吸尘器','飞利浦',999.00,9]]
products_cart=[]
option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
while option in['1','2','3',]:
    if option=='1':
        print('产品和价格信息如下')
        print('***********************************************************')
        print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量')
        print('-----------------------------------------------------------')
        for i in range(len(pro)):
            print('%-10s'%pro[i][0],'%-10s'%pro[i][1],'%-10s'%pro[i][2],'%10.2f'%pro[i][3],'%10d'%pro[i][4])

        print('-----------------------------------------------------------')
    elif option=='2':
        pro_id=input('请输入您要购买的产品编号:')
        while pro_id not in [item[0]for item in pro]:
            pro_id=input('编号不存在,请重新输入您要购买的产品编号:')
        count=int(input('请输入您要购买的产品数量:'))
        while count > pro[int(pro_id)-1][4]:
            count=input('数量超出库存。请重新输入您要购买的产品数量:')
        if pro_id not in [item[0] for item in products_cart]:
            products_cart.append([pro_id,count])
        else:
            for i in range(len(products_cart)):
                if products_cart[i][0]==pro_id:
                    products_cart[i][1]+=count
    else:
        print('购物车信息如下:')
        print('************************************')
        print('%-10s'%'编号','%-10s'%'购买数量')
        print('------------------------------------')
        for i in range(len(products_cart)):
            print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1])
        print('------------------------------------')
    option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
if len(products_cart)>0:
    amount=0
    for i in range(len(products_cart)):
        pro_index=0
        for j in range(len(pro)):
            if pro[j][0]==products_cart[i][0]:
                pro_index=j
                break
        price=pro[pro_index][3]
        count=products_cart[i][1]
        amount+=price*count
    if 5000 < amount <= 10000:
        amount=amount*0.95
    elif 10000 < amount <= 20000:
        amount = amount * 0.9
    elif 20000 < amount :
        amount = amount * 0.85
    else:
        amount=amount*1
    print('购买成功,您需要支付%8.2f元'%amount)
print('谢谢您的光临,下次再见!')

  

print('欢迎使用家用电器销售系统!')
pro=[['0001','电视机','海尔',5999.00,20],
          ['0002','冰箱','西门子',6998.00,15],
          ['0003','洗衣机','小天鹅',1999.00,10],
          ['0004','空调','格力',3900.00,0],
          ['0005','热水器','格力',688.00,30],
          ['0006','笔记本','联想',5699.00,10],
          ['0007','微波炉','苏泊尔',480.00,33],
          ['0008','投影仪','松下',1250.00,12],
          ['0009','吸尘器','飞利浦',999.00,9]]
products_cart=[]
option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
while option in['1','2','3',]:
    if option=='1':
        print('产品和价格信息如下')
        print('***********************************************************')
        print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号','名称','品牌','价格','库存数量'))
        print('-----------------------------------------------------------')
        for i in range(len(pro)):
            print('{:<10}'.format(pro[i][0]), '{:<10}'.format(pro[i][1]), '{:<10}'.format(pro[i][2]),
                  '{:<10.2f}'.format(pro[i][3]),
                  '{:>10}'.format(pro[i][4]))

        print('-----------------------------------------------------------')
    elif option=='2':
        pro_id=input('请输入您要购买的产品编号:')
        while pro_id not in [item[0]for item in pro]:
            pro_id=input('编号不存在,请重新输入您要购买的产品编号:')
        count=int(input('请输入您要购买的产品数量:'))
        while count > pro[int(pro_id)-1][4]:
            count=input('数量超出库存。请重新输入您要购买的产品数量:')
        if pro_id not in [item[0] for item in products_cart]:
            products_cart.append([pro_id,count])
        else:
            for i in range(len(products_cart)):
                if products_cart[i][0]==pro_id:
                    products_cart[i][1]+=count
    else:
        print('购物车信息如下:')
        print('************************************')
        print('{:<10} {:<10} '.format('编号','购买数量'))
        print('------------------------------------')
        for i in range(len(products_cart)):
            print('{:10s}'.format(products_cart[i][0]),'{:6d}'.format(products_cart[i][1]))
        print('------------------------------------')
    option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
if len(products_cart)>0:
    amount=0
    for i in range(len(products_cart)):
        pro_index=0
        for j in range(len(pro)):
            if pro[j][0]==products_cart[i][0]:
                pro_index=j
                break
        price=pro[pro_index][3]
        count=products_cart[i][1]
        amount+=price*count
    if 5000 < amount <= 10000:
        amount=amount*0.95
    elif 10000 < amount <= 20000:
        amount = amount * 0.9
    elif 20000 < amount :
        amount = amount * 0.85
    else:
        amount=amount*1
    print('购买成功您需要支付{:8.2f}元'.format(amount))
print('谢谢您的光临,下次再见!')

  

print('欢迎使用家用电器销售系统!')
pro=[{'id':'0001','name':'电视机','brand':'海尔','price':5999.00,'count':20},
     {'id':'0002','name':'冰箱','brand':'西门子','price':6998.00,'count':15},
     {'id':'0003','name':'洗衣机','brand':'小天鹅','price':1999.00,'count':10},
     {'id':'0004','name':'空调','brand':'格力','price':3900.00,'count':0},
     {'id':'0005','name':'热水器','brand':'格力','price':688.00,'count':30},
     {'id':'0006','name':'笔记本','brand':'联想','price':5699.00,'count':10},
     {'id':'0007','name':'微波炉','brand':'苏泊尔','price':480.00,'count':33},
     {'id':'0008','name':'投影仪','brand':'松下','price':1250.00,'count':12},
     {'id':'0009','name':'吸尘器','brand':'飞利浦','price':999.00,'count':9}
     ]
products_cart=[]
option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
while option in['1','2','3',]:
    if option=='1':
        print('产品和价格信息如下')
        print('***********************************************************')
        print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量')
        print('-----------------------------------------------------------')
        for i in range(len(pro)):
            print('%-10s'%pro[i]['id'],'%-10s'%pro[i]['name'],'%-10s'%pro[i]['brand'],'%10.2f'%pro[i]['price'],'%10d'%pro[i]['count'])
        print('-----------------------------------------------------------')
    elif option == '2':
        pro_id = input('请输入您要购买的产品编号:')
        while pro_id not in [item['id'] for item in pro]:
            pro_id = input('编号不存在,请重新输入您要购买的产品编号:')
        count=int(input('请输入您要购买的产品数量:'))
        while count > pro[int(pro_id)-1]['count']:
            count=input('数量超出库存。请重新输入您要购买的产品数量:')
        if pro_id not in [item['id'] for item in products_cart]:
            products_cart.append({'id':pro_id,'count':count})
        else:
            for i in range(len(products_cart)):
                if products_cart[i].get('id')==pro_id:
                    products_cart[i]['count']+=count

        for i in range(len(pro)) :
            if pro[i]['id']==pro_id:
                pro[i]['count']-=count
    else:
        print('购物车信息如下:')
        print('************************************')
        print('%-10s'%'编号','%-10s'%'购买数量')
        print('------------------------------------')
        for i in range(len(products_cart)):
            print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count'])
        print('------------------------------------')
    option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
if len(products_cart)>0:
    amount=0
    for i in range(len(products_cart)):
        pro_index=0
        for j in range(len(pro)):
            if pro[j]['id']==products_cart[i]['id']:
                pro_index=j
                break
        price=pro[pro_index]['price']
        count=products_cart[i]['count']
        amount+=price*count
    if 5000 < amount <= 10000:
        amount = amount * 0.95
    elif 10000 < amount <= 20000:
        amount = amount * 0.9
    elif 20000 < amount:
        amount = amount * 0.85
    else:
        amount = amount * 1
    print('购买成功,您需要支付%8.2f元' % amount)
print('谢谢您的光临,下次再见!')

  

print('欢迎使用家用电器销售系统!')
pro=[{'id':'0001','name':'电视机','brand':'海尔','price':5999.00,'count':20},
     {'id':'0002','name':'冰箱','brand':'西门子','price':6998.00,'count':15},
     {'id':'0003','name':'洗衣机','brand':'小天鹅','price':1999.00,'count':10},
     {'id':'0004','name':'空调','brand':'格力','price':3900.00,'count':0},
     {'id':'0005','name':'热水器','brand':'格力','price':688.00,'count':30},
     {'id':'0006','name':'笔记本','brand':'联想','price':5699.00,'count':10},
     {'id':'0007','name':'微波炉','brand':'苏泊尔','price':480.00,'count':33},
     {'id':'0008','name':'投影仪','brand':'松下','price':1250.00,'count':12},
     {'id':'0009','name':'吸尘器','brand':'飞利浦','price':999.00,'count':9}
     ]
products_cart=[]
option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
while option in['1','2','3',]:
    if option=='1':
        print('产品和价格信息如下')
        print('***********************************************************')
        print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号','名称','品牌','价格','库存数量'))
        print('-----------------------------------------------------------')
        for i in range(len(pro)):
            print('{:<10}'.format(pro[i]['id']), '{:<10}'.format(pro[i]['name']), '{:<10}'.format(pro[i]['brand']),
                  '{:<10.2f}'.format(pro[i]['price']),
                  '{:>10}'.format(pro[i]['count']))
        print('-----------------------------------------------------------')
    elif option == '2':
        pro_id = input('请输入您要购买的产品编号:')
        while pro_id not in [item['id'] for item in pro]:
            pro_id = input('编号不存在,请重新输入您要购买的产品编号:')
        count=int(input('请输入您要购买的产品数量:'))
        while count > pro[int(pro_id)-1]['count']:
            count=input('数量超出库存。请重新输入您要购买的产品数量:')
        if pro_id not in [item['id'] for item in products_cart]:
            products_cart.append({'id':pro_id,'count':count})
        else:
            for i in range(len(products_cart)):
                if products_cart[i].get('id')==pro_id:
                    products_cart[i]['count']+=count

        for i in range(len(pro)) :
            if pro[i]['id']==pro_id:
                pro[i]['count']-=count
    else:
        print('购物车信息如下:')
        print('************************************')
        print('{:<10} {:<10} '.format('编号','购买数量'))
        print('------------------------------------')
        for i in range(len(products_cart)):
            print('{:10s}'.format(products_cart[i]['id']),'{:6d}'.format(products_cart[i]['count']))
        print('------------------------------------')
    option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
if len(products_cart)>0:
    amount=0
    for i in range(len(products_cart)):
        pro_index=0
        for j in range(len(pro)):
            if pro[j]['id']==products_cart[i]['id']:
                pro_index=j
                break
        price=pro[pro_index]['price']
        count=products_cart[i]['count']
        amount+=price*count
    if 5000 < amount <= 10000:
        amount = amount * 0.95
    elif 10000 < amount <= 20000:
        amount = amount * 0.9
    elif 20000 < amount:
        amount = amount * 0.85
    else:
        amount = amount * 1
    print('购买成功您需要支付{:8.2f}元'.format(amount))
print('谢谢您的光临,下次再见!')

  

 

标签:count,pro,cart,实验,print,products,id
From: https://www.cnblogs.com/yhyy/p/17332352.html

相关文章

  • 实验3 控制语句与组合数据类型应用编辑
    实验任务1task1程序源代码1#task1_py2print('task1_py')34importrandom56print('用列表存取随机整数:')7lst=[random.randint(0,100)foriinrange(5)]8print(lst)910print('\n用集合存储随机整数:')11s1={random.randint(0,100......
  • 实验3 控制语句和组合数据类型应用编程
    实验任务1task_1.py实验源码:1importrandom2print('用列表存储随机整数:')3lst=[random.randint(0,100)foriinrange(5)]4print(lst)5print('\n用集合存储随机整数:')6s1={random.randint(0,100)foriinrange(5)}7print(s1)8print(......
  • 实验3 控制语句与组合数据类型应用编程
    importrandomprint('用列表储存随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用整数储存随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合储存随机整数:')s2=set()whilelen(s2)&......
  • 实验3 控制语句与组合数据类型应用编程
    importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(s2)......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1#task1实验内容importrandomprint('用列表存取随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)......
  • 实验三
    task1源代码:1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\......
  • 实验3
    实验3.1:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()while......
  • C语言实验报告范例
    实验报告四一,实验类型:设计型二,实验室:三,指导老师:四,日期:五,实验名称:if分支语句的嵌套六,实验目的:1,学习if嵌套结构,能够用C语言编程解决日常生活的实例2,明确if语句在实现分支结构控制语句方面的特点和优势3,熟练掌握关系运算符、关系表达式、逻辑运算符和逻辑表达式、......
  • 实验一 进程的调度
    对一些函数和知识的解释getpid():返回当前进程的PID(进程编号)pid_tC语言重新定义的数据类型(其实就是int,换了一个名字)用来存储进程的PID。pid_t定义在sys/types.h头文件中fork()函数:用于创建一个新进程作为当前进行的子进程。如果成功创建⼦进程,对于⽗⼦进程fork会返回......
  • 逆向-第三次实验-注册码分析
    本次实验考虑使用到了ida在使用ida时,用ida32而非64,否则无法查看伪代码。打开后看到dialog,即对话框元素函数,很有可能是我们要考虑的地方,点击后F5得到伪代码。 代码INT_PTR__stdcallDialogFunc(HWNDhWnd,UINTa2,WPARAMa3,LPARAMa4){HICONIconA;//eaxUINT......