码迷,mamicode.com
首页 > 其他好文 > 详细

购物车

时间:2019-12-15 12:29:20      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:hide   none   nbsp   print   event   ==   range   username   closed   

 

 

技术图片
import sys

# 商品信息:
goods = [
    {‘name‘: ‘电脑‘, ‘price‘: 1999},
    {‘name‘: ‘鼠标‘, ‘price‘: 10},
    {‘name‘: ‘游艇‘, ‘price‘: 18888},
    {‘name‘: ‘键盘‘, ‘price‘: 20}
]

# 用户信息:
user = {}

# 购物车:
shopping = []

y = ‘‘‘
1) 登录
2)查看商品
3)退出
‘‘‘

print(y)

option = input(‘请输入你要进行的操作:‘)
if option == ‘1‘:
    username = input(‘请输入用户名:‘)
    password = input(‘请输入密码:‘)
    salary = input(‘请输入你的工资:‘)
    user = {‘username‘: username, ‘password‘: password, ‘salary‘: int(salary)}
    print(‘欢迎用户:{}登录成功!‘.format(username))
    option2 = input(‘请选择:1) 查看商品  2) 退出程序‘)
    if option2 == ‘1‘:
        if len(user) != 0:
            for i in range(len(goods)):
                print(i + 1, goods[i][‘name‘], goods[i][‘price‘])
            while 1:
                option3 = input(‘请输出你要购买的商品或退出:‘)
                if option3 == ‘退出‘:
                    print(‘已购买:{},还剩余额:{}‘.format(shopping, user[‘salary‘]))
                    sys.exit()
                else:
                    if user[‘salary‘] > goods[int(option3)-1][‘price‘]:
                        shopping.append(goods[int(option3) - 1])
                        user[‘salary‘] = user[‘salary‘] - goods[int(option3) - 1][‘price‘]
                        print(user)
                        print(shopping)
    else:
        sys.exit()

elif option == ‘2‘:
    if len(user) != 0:
        pass
    else:
        print(‘你还没有登录,请先登录‘)
        sys.exit()

else:
    sys.exit()
View Code

购物车

标签:hide   none   nbsp   print   event   ==   range   username   closed   

原文地址:https://www.cnblogs.com/lw1095950124/p/12043689.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!