概述:
今天主要分享制作购物车程序。
功能:
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>
结果:
觉得有用的朋友多帮忙转发!后面更多运维技术分享内容,感兴趣的朋友可以关注一下!!
閱讀更多 愛踢人生 的文章