练习:

  1. 要求用户输入总资产,例如:2000

  2. 显示商品列表,让用户根据序号选择商品,加入购物车

  3. 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。

goods = [{'name':'电脑', 'price':1999},{'name':'鼠标', 'price':10},{'name':'游艇', 'price':20},{'name':'玩具', 'price':998},]
p1 = int(input('enter your money:'))

while True:
    if not isinstance(p1, int):
        raise TypeError('bad type.')
    for i in goods:
        print(goods.index(i), i)
    p2 = int(input('enter your price:'))
    if p2 == 0:
        p1 = p1 - int(goods[p2]['price'])
        if p1 < 0:
            print('余额不足。')
            break
        else:
            print('***********************')
            print('当前余额:', p1)
            print('***********************')
    elif p2 == 1:
        p1 = p1 - int(goods[p2]['price'])
        # print(p3)
        if p1 < 0:
            print('余额不足。')
            break
        else:
            print('***********************')
            print('当前余额:', p1)
            print('***********************')
    elif p2 == 2:
        p1 = p1 - int(goods[p2]['price'])
        # print(p3)
        if p1 < 0:

            print('余额不足。')
            break
        else:
            print('***********************')
            print('当前余额:', p1)
            print('***********************')
    elif p2 == 3:
        p1= p1 - int(goods[p2]['price'])
        # print(p3)
        if p1 < 0:
            print('余额不足。')
            break
        else:
            print('***********************')
            print('当前余额:', p1)
            print('***********************')
    else:
        print('***********************')
        print('商品序号错误,请重新输入。')
        print('***********************')
View Code

相关文章:

  • 2021-09-16
  • 2022-12-23
  • 2021-08-10
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
猜你喜欢
  • 2021-07-14
  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
  • 2021-11-26
  • 2021-12-06
相关资源
相似解决方案