1. 程式人生 > >Python 之 購物車程序(列表使用場景)

Python 之 購物車程序(列表使用場景)

auth print put 打印 use chan pytho item author

要求:

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("輸入有誤,請重新輸入")

Python 之 購物車程序(列表使用場景)