shopping car 3.0
生活随笔
收集整理的這篇文章主要介紹了
shopping car 3.0
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : 購物車3.0.py
# @Author: Anthony.waa
# @Date : 2018/5/14 0014
# 原始購物清單
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標(biāo)", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 購物車
shopping_car = {}
# 商品個數(shù)
good_count = 1
# 輸入不合法是打印
def return_error():
print("\033[1;31m 輸入不合法,請重新輸入 \033[0m")
# 打印購物車商品和剩余金額
def end_shopping():
print("\033[1;32m 剩余金額為: \033[0m", all_money)
print('\033[1;32m 已購商品有: \033[0m', shopping_car, )
while True:
# 用戶輸入總資產(chǎn)
all_money = input("\033[1;32m 請輸入總資產(chǎn): \033[0m").strip()
if all_money.isdigit():
all_money = int(all_money)
# 顯示購物商品列表
print('\033[1;32m ========== 商 品 清 單 ========== \033[0m'.center(20))
for good_index, good in enumerate(goods, 1):
print('\033[1;32m {0} {1} {2} \033[0m'.format(good_index, good['name'],good['price']).center(20))
print('\033[1;32m 選擇"q"退出 \033[0m'.center(20))
while True:
# 選擇商品序號,或選擇退出購物
choice_count = input("\033[1;32m 請輸入商品序號: \033[0m").strip()
if choice_count.isdigit():
choice_count = int(choice_count)
# 商品序號小于商品列表長度
if choice_count > 0 and choice_count <= len(goods):
good_price = goods[choice_count - 1]['price']
# 用戶資產(chǎn)大于商品價格,否則提示充值
if all_money > good_price:
good_name = goods[choice_count - 1]['name']
all_money -= good_price
# 商品是否存在購物車中
if good_name not in shopping_car:
shopping_car[good_name] = good_count
else:
for shopping_index, shopping_name in enumerate(shopping_car):
if shopping_name == good_name:
shopping_car[shopping_name] += 1
end_shopping()
else:
print('\033[1;31m 余額不足,請充值: \033[0m'.center(17))
add_money = input("請輸入總資產(chǎn):").strip()
if add_money.isdigit():
add_money = int(add_money)
all_money += add_money
print('\033[1;32m 充值成功,剩余金額為: \033[0m'.center(17), all_money)
continue
elif choice_count.lower() == 'q':
print("\033[1;32m 購物結(jié)束,歡迎下次光臨 \033[0m")
end_shopping()
exit()
return_error()
# -*- coding: utf-8 -*-
# @File : 購物車3.0.py
# @Author: Anthony.waa
# @Date : 2018/5/14 0014
# 原始購物清單
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標(biāo)", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 購物車
shopping_car = {}
# 商品個數(shù)
good_count = 1
# 輸入不合法是打印
def return_error():
print("\033[1;31m 輸入不合法,請重新輸入 \033[0m")
# 打印購物車商品和剩余金額
def end_shopping():
print("\033[1;32m 剩余金額為: \033[0m", all_money)
print('\033[1;32m 已購商品有: \033[0m', shopping_car, )
while True:
# 用戶輸入總資產(chǎn)
all_money = input("\033[1;32m 請輸入總資產(chǎn): \033[0m").strip()
if all_money.isdigit():
all_money = int(all_money)
# 顯示購物商品列表
print('\033[1;32m ========== 商 品 清 單 ========== \033[0m'.center(20))
for good_index, good in enumerate(goods, 1):
print('\033[1;32m {0} {1} {2} \033[0m'.format(good_index, good['name'],good['price']).center(20))
print('\033[1;32m 選擇"q"退出 \033[0m'.center(20))
while True:
# 選擇商品序號,或選擇退出購物
choice_count = input("\033[1;32m 請輸入商品序號: \033[0m").strip()
if choice_count.isdigit():
choice_count = int(choice_count)
# 商品序號小于商品列表長度
if choice_count > 0 and choice_count <= len(goods):
good_price = goods[choice_count - 1]['price']
# 用戶資產(chǎn)大于商品價格,否則提示充值
if all_money > good_price:
good_name = goods[choice_count - 1]['name']
all_money -= good_price
# 商品是否存在購物車中
if good_name not in shopping_car:
shopping_car[good_name] = good_count
else:
for shopping_index, shopping_name in enumerate(shopping_car):
if shopping_name == good_name:
shopping_car[shopping_name] += 1
end_shopping()
else:
print('\033[1;31m 余額不足,請充值: \033[0m'.center(17))
add_money = input("請輸入總資產(chǎn):").strip()
if add_money.isdigit():
add_money = int(add_money)
all_money += add_money
print('\033[1;32m 充值成功,剩余金額為: \033[0m'.center(17), all_money)
continue
elif choice_count.lower() == 'q':
print("\033[1;32m 購物結(jié)束,歡迎下次光臨 \033[0m")
end_shopping()
exit()
return_error()
轉(zhuǎn)載于:https://www.cnblogs.com/ipyanthony/p/9066288.html
總結(jié)
以上是生活随笔為你收集整理的shopping car 3.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VBScript学习笔记 - 数组
- 下一篇: Android(java)学习笔记144