python之購物車程序

概述:

今天主要分享製作購物車程序。

功能:

1.啟動程序後,讓用戶輸入工資,然後打印商品列表

2.允許用戶根據商品編號購買商品

3.用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒

4.可隨時退出,退出時,打印已購買商品和餘額

實現:

<code>#!/usr/bin/env python  
# -*- coding: utf8 -*-
# @Time : 2020/3/15 10:13
__author__ = 'andy chen'

product_list = [
('Iphone', 5800), ('Mac Pro', 9800), ('Watch', 10600), ('Bike', 800), ('Caffee', 31), ('Alex Python', 120),]
shopping_list = []

salary = input("Input your salary>>>:")
if salary.isdigit():
salary = int(salary)
while True:
for index, item in enumerate(product_list):
print(index, item)
user_choice = input("選擇要買嘛?>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart,your current balance is \\033[31;1m%s\\033[0m " % (p_item, salary))
else:
print("\\033[32;1m你的餘額還剩%s,還買毛線啊.\\033[0m" % salary)
else:
print("你選擇的產品不存在!")
elif user_choice == 'q':
print("------------------shopping list---------------")
for p in shopping_list:
print(p)
print("your current balance:", salary)
exit()
else:
print("Invalid option!")
else:
print("error!!")/<code>

結果:


python之購物車程序


python之購物車程序


覺得有用的朋友多幫忙轉發!後面更多運維技術分享內容,感興趣的朋友可以關注一下!!


分享到:


相關文章: