首页 > 其他分享 >实验三

实验三

时间:2023-04-25 22:33:16浏览次数:39  
标签:count format cart products print 实验 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:范围[1,100]闭区间,可以取到100

问题2:[0,4]闭区间,不包括5

             [1,4]闭区间,不包括5

问题3:不一定,集合没有顺序,元素随机排列

            不一定,集合没有顺序

book_info = {'isbn': '978-7-5356-8297-0',
             '书名': 'naijingji',
             'zuozhe': 'krithdof.xiabute',
             'translater': 'gaowenjing',
             'chubanshe': 'hunanmeishu',
             'price': 82
            }
for key, value in book_info.items():
    print(f'{key}: {value}')
print()

for item in book_info.items():
    print(f'{item[0]}: {item[1]}')
print()

for value in book_info.values():
    print(value, end=' ')
print()

for key in book_info.keys():
    print(book_info[key],end=' ')
print()

 

book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
              {'书名': '局外人', '作者': '阿尔贝.加缪'},
              {'书名': '设计中的设计', '作者': '原研哉'},
              {'书名': '万历十五年', '作者': '黄仁宇'},
              {'书名': '刀锋', '作者': '毛姆'}
             ]
i = 0
x = list()
while i < len(book_infos):
    for value in book_infos[i].values():
        x.append(value)
    i += 1
i = 0
s1 = dict()
while i < len(x):
    s1[ '《' + x[i] + '》']=x[i+1]
    i+= 2
y = list()
for key, value in s1.items():
    y.append(f'{key}, {value}')
i = 0
while i < len(y):
    print( str(i+1) + '.' + ' ' + y[i])
    i += 1

 

x = '''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!
'''
x.lower()
y = dict()
for i in range(ord('a'),ord('z')+1):
    y[chr(i)] = 0
for i in x:
    if (ord(i)>=ord('a') and ord(i)<=ord('z')):
        y[i] = x.count(i)
    else:
        continue
z = list(y.items())
z.sort(key=lambda y: y[1],reverse=True)
for i in dict(z):
    print(f'{i}:{dict(z)[i]}')
print('专业代号信息'.center(50, '-'))
y = {8326: '地信类', 8329: '计算机类', 8330: '气科类', 8336: '防灾工程', 8345: '海洋科学', 8382: '气象工程'}
for i in y:
    print(f'{i}:{y[i]}')
print('学生专业查询'.center(50,'-'))
while True:
    x = str(input('情输入学号:'))
    if x == '#':
        break
    elif int(x[4:8]) in y.keys():
        print(f'专业是:{y[int(x[4:8])]}')
    else:
        print('不在这些专业中...')
print('查询结束...')

 

 

import random
lucky_day = random.randint(1,31)
print('猜猜2023年5月哪一天会是你的lucky day','\U0001f609')
a = int(input('你有三次机会,猜吧(1~31):'))
i = 0
while i in range(0,2):
    if a != lucky_day:
        i += 1
        if a not in range(1,32):
            print('地球上没有这一天啦,你是外星人吗')
        elif a < lucky_day:
            print('猜早啦,你的luckyday还没到呢')
        elif a > lucky_day:
            print('猜晚啦,你的luckyday已经过了')
        a = int(input('再猜(1~31):'))
        if i == 2:
            if a < lucky_day:
                print('猜早啦,你的luckyday还没到呢')
            elif a > lucky_day:
                print('猜晚啦,你的luckyday已经过了')
            print('哇哦,次数用光啦.')
            print(f'偷偷告诉你,5月你的lucky day是{lucky_day}.good luck','\U0001F642')
    else:
        print('哇,猜中了','\U0001F923')
        i += 3

 

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

 

print('欢迎使用家用电器销售系统!')

products = [
['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.50,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('{:*^48}'.format('*'))
        print('{:10}'.format('编号'), '{:10}'.format('名称'), '{:10}'.format('品牌'), '{:10}'.format('价格'), '{:10}'.format('库存数量'))
        print('{:-^50}'.format('-'))
        for i in range(len(products)):
            print('{:10}'.format(products[i][0]), '{:10}'.format(products[i][1]), '{:10}'.format(products[i][2]), '{:10}'.format(products[i][3]), '{:10}'.format(products[i][4]))
        print('{:-^50}'.format('-'))
    elif option == '2':
        product_id = input('请输入您要购买的产品编号:')
        while product_id not in [item[0] for item in products]:
            product_id = input('编号不存在,请重新输入您要购买的产品编号:')

        count = int(input('请输入您要购买的产品数量:'))
        while count > products[int(product_id)-1][4]:
            count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
        if product_id not in [item[0] for item in products_cart]:
            products_cart.append([product_id,count])
        else:
            for i in range(len(products_cart)):
                if products_cart[i][0] == product_id:
                    products_cart[i][1]+=count
        for i in range(len(products)):
            if products[i][0] == product_id:
                products[i][4] -= count
    else:
        print('购物车信息如下:')
        print('{:*^48}'.format('*'))
        print('{:10}'.format('编号'), '{:10}'.format('购买数量'))
        print('{:-^50}'.format('-'))
        for i in range(len(products_cart)):
            print('{:10}'.format(products_cart[i][0]), '{:6}'.format(products_cart[i][1]))
        print('{:-^50}'.format('-'))
    option = input('操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
if len(products_cart) > 0:
    amount = 0
    for i in range(len(products_cart)):
        product_index = 0
        for j in range(len(products)):
            if products[j][0] == products_cart[i][0]:
                product_index = j
                break
        price = products[product_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.90
    elif amount > 20000:
        amount = amount*0.85
    else:
        amount = amount*1
    print('购买成功,您需要支付%8.2f元' % amount)
print('谢谢您的光临,下次再见!')

 

print('欢迎使用家用电器销售系统!')
products = [
['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.50,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('{:*^48}'.format('*'))
        print('{:10}'.format('编号'), '{:10}'.format('名称'), '{:10}'.format('品牌'), '{:10}'.format('价格'), '{:10}'.format('库存数量'))
        print('{:-^50}'.format('-'))
        for i in range(len(products)):
            print('{:10}'.format(products[i][0]), '{:10}'.format(products[i][1]), '{:10}'.format(products[i][2]), '{:10}'.format(products[i][3]), '{:10}'.format(products[i][4]))
        print('{:-^50}'.format('-'))
    elif option == '2':
        product_id = input('请输入您要购买的产品编号:')
        while product_id not in [item[0] for item in products]:
            product_id = input('编号不存在,请重新输入您要购买的产品编号:')

        count = int(input('请输入您要购买的产品数量:'))
        while count > products[int(product_id)-1][4]:
            count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
        if product_id not in [item[0] for item in products_cart]:
            products_cart.append([product_id,count])
        else:
            for i in range(len(products_cart)):
                if products_cart[i][0] == product_id:
                    products_cart[i][1]+=count
        for i in range(len(products)):
            if products[i][0] == product_id:
                products[i][4] -= count
    else:
        print('购物车信息如下:')
        print('{:*^48}'.format('*'))
        print('{:10}'.format('编号'), '{:10}'.format('购买数量'))
        print('{:-^50}'.format('-'))
        for i in range(len(products_cart)):
            print('{:10}'.format(products_cart[i][0]), '{:6}'.format(products_cart[i][1]))
        print('{:-^50}'.format('-'))
    option = input('操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')

if len(products_cart) > 0:
    amount = 0
    for i in range(len(products_cart)):
        product_index = 0
        for j in range(len(products)):
            if products[j][0] == products_cart[i][0]:
                product_index = j
                break
        price = products[product_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.90
    elif amount > 20000:
        amount = amount*0.85
    else:
        amount = amount*1
    print('购买成功,您需要支付%8.2f元' % amount)
print('谢谢您的光临,下次再见!')

 

print('欢迎使用家用电器销售系统!')
products=[
  {'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':580.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(f'{"":*>48s}')
        print(f'{"编号":10s}{"名称":10s}{"品牌":10s}{"价格":10s}{"库存数量":10s}')
        print(f'{"":->50s}')
        for i in range(len(products)):
            print('{:10}'.format(products[i]['id']),'{:10}'.format(products[i]['name']),'{:10}'.format(products[i]['brand']),'{:10}'.format(products[i]['price']))
        print(f'{"":->50s}')
    elif option == '2':
        product_id = input('请输入您要购买的商品编号:')
        while product_id not in [item['id'] for item in products]:
            product_id = input('编号不存在,请重新输入您要购买的产品编号:')
        count = int(input('请输入您要购买的产品数量:'))
        while count > products[int(product_id)-1]['count']:
            count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
        if product_id not in [item['id'] for item in products_cart]:
            products_cart.append({'id':product_id,'count':count})
        else:
            for i in range(len(products_cart)):
                if products_cart[i].get('id') == product_id:
                    products_cart[i]['count'] += count
        for i in range(len(products)):
            if products[i]['id'] == product_id:
                products[i]['count'] -= count
    else:
        print('购物车信息如下')
        print(f'{"":*>48s}')
        print(f'{"编号":10s}{"购买数量":10s}')
        print(f'{"":->50s}')
        for i in range(len(products_cart)):
            print('{:10}'.format(products_cart[i]['id']),'{:6}'.format(products_cart[i]['count']))
        print(f'{"":->50s}')
    option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
    if len(products_cart)>0:
        amount=0
        for i in range(len(products_cart)):
            product_index = 0
            for j in range(len(products)):
                if products[j]['id'] == products_cart[i]['id']:
                    product_index = j
                    break
            price = products[product_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.90
            elif amount > 20000:
                amount = amount*0.85
            else:
                amount = amount*1
            print('购买成功,您需要支付%8.2f'%amount)
        print('谢谢您的光临,下次再见!')
print('欢迎使用家用电器销售系统!')
products=[
  {'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':580.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(f'{"":*>48s}')
        print(f'{"编号":10s}{"名称":10s}{"品牌":10s}{"价格":10s}{"库存数量":10s}')
        print(f'{"":->50s}')
        for i in range(len(products)):
            print('{:10}'.format(products[i]['id']),'{:10}'.format(products[i]['name']),'{:10}'.format(products[i]['brand']),'{:10}'.format(products[i]['price']))
        print(f'{"":->50s}')
    elif option == '2':
        product_id = input('请输入您要购买的商品编号:')
        while product_id not in [item['id'] for item in products]:
            product_id = input('编号不存在,请重新输入您要购买的产品编号:')
        count = int(input('请输入您要购买的产品数量:'))
        while count > products[int(product_id)-1]['count']:
            count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
        if product_id not in [item['id'] for item in products_cart]:
            products_cart.append({'id':product_id,'count':count})
        else:
            for i in range(len(products_cart)):
                if products_cart[i].get('id') == product_id:
                    products_cart[i]['count'] += count
        for i in range(len(products)):
            if products[i]['id'] == product_id:
                products[i]['count'] -= count
    else:
        print('购物车信息如下')
        print(f'{"":*>48s}')
        print(f'{"编号":10s}{"购买数量":10s}')
        print(f'{"":->50s}')
        for i in range(len(products_cart)):
            print('{:10}'.format(products_cart[i]['id']),'{:6}'.format(products_cart[i]['count']))
        print(f'{"":->50s}')
    option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
    if len(products_cart)>0:
        amount=0
        for i in range(len(products_cart)):
            product_index = 0
            for j in range(len(products)):
                if products[j]['id'] == products_cart[i]['id']:
                    product_index = j
                    break
            price = products[product_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.90
            elif amount > 20000:
                amount = amount*0.85
            else:
                amount = amount*1
            print('购买成功,您需要支付%8.2f'%amount)
        print('谢谢您的光临,下次再见!')

 

 

标签:count,format,cart,products,print,实验,id
From: https://www.cnblogs.com/zsbd/p/17354165.html

相关文章

  • 实验1
    task1_1.py1print('hey,u')2print('hey','u')3x,y,z=1,2,34print(x,y,z)5print('x=%d,y=%d,z=%d'%(x,y,z))6print('x={},y={},z={}'.format(x,y,z))7print(f'x={x},y={y},z={z}')......
  • 实验3 控制语句与组合数据类型应用编程
    task1程序源码:1importrandom23print('用列表储存随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\......
  • Python实验课3
    实验任务1importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()wh......
  • 实验三
    实验任务1task1:1importrandom23print('用列表存储:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集合'......
  • 实验3
    1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集合存储随机......
  • 实验三
    1.实验任务1task1.py程序源代码:1importrandom2print('用列表存储随机整数:')3lst=[random.randint(0,100)foriinrange(5)]4print(lst)5print('\n用集合存储随机整数:')6s1={random.randint(0,100)foriinrange(5)}7print(s1)8print('\......
  • 实验3 控制语句与组合数据类型应用编程
    task1.pyimportrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whi......
  • python实验笔记1
    1.python如何在一行里面输入两个数呢如果直接这样子写会报错n=int(input())m=int(input())要按照下面的写法才可以实现n,m=map(int,input().split())2.python实现排列组合在itertools库中提供了两个函数permutations和combinations可以实现全排列和组......
  • 实验三
    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(......
  • 山东大学数据结构实验七
    卡片游戏tips:这个题还要参考,同学要加油啦~~要求创建队列类,使用数组描述的循环队列实现卡片游戏描述假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。......