[作业] Python入门基础---购物车小程序

1.购物车小程序:

1.1用户输入工资取60%

1.2打印输出商品菜单

1.3由用户输入数字选择

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#__author:Mifen
#date: 2018/11/27
 
# 购物车程序
 
 
#把工资作为账户的余额
salary = int (input('你的工资为:'))
funds = salary * 0.6  # 取工资的60%
 
#自定义本地商品数据列表[商品名称,价格,库存]
menu = [['保留使用,不存数据'],['iPhone7',6000,30],['Notebook',9000,30],['coffee',32,30],['Pythonbook',80,30],['bike',2000,30]]
count = len(menu) #统计本地商品种类数
#定义一个空列表存储购买商品
shopping_cart = []
 
for i in range(1,count): #循环获取列表数据
    #输出商品信息
    print('''%d.商品名称:%s
             价格:%d
             库存:%d'''
              % (i,menu[i][0],menu[i][1],menu[i][2]),end='\t')
    print()
 
print('账户余额¥%.2f' % funds)
user_select = int(input('输入购买的商品前的序号(886退出):'))
 
while True:
 
    if 0<user_select < count:
        print('''商品名称:%s \n价格¥:%d \n库存:%d'''
              % (menu[user_select][0], menu[user_select][1], menu[user_select][2])) #显示该商品信息
        if funds < menu[user_select][1]: #判断余额是否能买一件改商品
            print('%s 单价为¥ %d/件,当前余额¥ %d' % (menu[user_select][0],menu[user_select][1],funds))
            user_select = int(input('请重新输入购买的商品前的序号(886退出):'))
            continue
        else:
            if menu[user_select][2]: #判断是否还有库存
                buy = int(input('请输入购买数量:'))
                price = int(menu[user_select][1]) * buy #总价
                if price < funds: #判断余额是否够付款
                    entrepot = menu[user_select][2] - buy  # 库存减少
                    menu[user_select][2] = entrepot  # 更新库存
                    shopping_cart.append([menu[user_select][0],menu[user_select][1],buy,price])#添加到购物车
                    funds = funds - price #结算
                    print('结算成功,当前余额为¥ %d' % funds)
                    user_select = int(input('输入购买的商品前的序号(886退出):'))
                else:
                    print('%d 件 %s 总价为¥ %d ,当前余额¥ %d,重新选择' % (buy,menu[user_select][0], price, funds))
                    continue
            else:
                print('库存不足')
                user_select = int(input('输入购买的商品前的序号(886退出):'))
    elif user_select == 886:
        print('多谢惠顾!')
        break
    else:
        print('Wrong input')
        user_select = int(input('输入购买的商品前的序号(886退出):'))

  

 

posted @   Amd794  阅读(274)  评论(1编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示