温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python 之 购物车程序(列表使用场景)

发布时间:2020-08-06 10:55:13 来源:网络 阅读:450 作者:wx592bc92b285c7 栏目:编程语言

要求:

1、程序运行时,让用户输入工资大小。

2、列出当所有产品列表清单。

3、让用户输入需要购买的产品编号。

4、结束程序时,打印购买明细与剩下余额。

#Author Kang

shopping_list = [('Iphone',5000),('MacBook',9000),('Huwei P20',9999)]

shopping_car = []

salary = int(input('请输入你的工资:'))

while True:
    for index,item in enumerate(shopping_list):
        print(index,item)
    user_change = input('请输入你要购买的产品编号:')
    if user_change.isdigit():
        user_change=int(user_change)
        if user_change >= 0 and user_change < len(shopping_list) and salary >= shopping_list[user_change][1]:
            shopping_car.append(shopping_list[user_change])
            salary -=shopping_list[user_change][1]
        elif salary <= shopping_list[user_change][1] :
            print('你的余额已经不足!!!')
        else:
            print('你输入的编号有误,请重新输入!!!!!')
    elif user_change == 'q':
        print('--------已购买的产品-------')
        print(shopping_car)
        print('剩下的余额为>>>:',salary)
        exit()
    else:
        print("输入有误,请重新输入")
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI