week 01

2. completion of a shopping mall car program.
Requirements :
1, users give their own account charge money: for example, to charge 3000 yuan.
2, has a form as follows :
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},]
3, page number display + Product Name + commodity prices, such as:
1 PC 1999
2 mouse 10
4, a user input selection of goods number, then print trade names and commodity prices , and this product, add to shopping cart ( own custom shopping
Car ) , the user can continue to add products.
5, if the user enters the product number is incorrect, you are prompted an error, and re-enter.
6, user input N to cart settlement, in order to display the user inside the shopping cart of goods, quantity and unit price, if less recharge amount of money, then let
User deletes an item until it can be purchased, if sufficient number of prepaid money, you can buy.
7, a user input Q or q exits the program.
8, then exit the program, in order to display the user to buy goods, quantity, unit price, and the total of how much money to spend, how many account balances, and
The purchase information is displayed.
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},]
wallet = 0 # wallet
shopping_cart1 = [] # cart
while True :
print("Welcome!".center(50, "-"))
chose = input ( " Please select mode q / Q: exit the program , 1: customer mode \ n >>>") # mode selection
if chose.upper() == "Q":
break
elif chose == "1":
flag = True # used to control the normal end of the loop , direct execution
Line else content behind
print (f " your current account balance is : {wallet} yuan .")
while flag:
chose_2 = input ( " Please select Q / q: exit the program , 1: charge money , 2: View and commodity Add to Cart
\n>>>")
if chose_2.upper() == "Q":flag = False
elif chose_2 == "1":
while True :
money = input ( " enter the recharge amount (Q / q to exit )")
if money.isdecimal() and int(money)>0:
# Input a positive integer not have other characters
wallet += int(money)
print (f " your current account balance is : {wallet} yuan .")
break #
Top-end exit recharge
elif money.upper() == "Q":
break # user decides to retire
Out
else:
print ( " format you entered is incorrect, please re-enter !")
elif chose_2 == "2":
shopping_cart = [] # with a list to accept each element of each of which are shopping information
Each dictionary dictionary contains name price number
while True:
for i in range (len (goods )): # show existing commodity
print(i + 1, goods[i]["name"], goods[i]["price"])
choose = input("请选择商品的序号:(输入Nn去结算)\n>>>")
if choose.upper() != "N":
if choose.isdecimal() and 0 < int(choose) < len(goods) +
1: #判断合法输入商品的序号
print(goods[int(choose) - 1]["name"],
goods[int(choose) - 1]["price"]) #打印选择的商品
count1 = 0
for i in range(len(shopping_cart)): #往购物车添加
if shopping_cart[i]["name"] ==
goods[int(choose) - 1]["name"]: #判断是不是已经有了此商品
count1 = 1
shopping_cart[i]["number"] +=1 #找到商品 数量
+1
#print(shopping_cart)
if count1 == 0: #没有在购物车里找到这个商品 ,直接添加到购物
shopping_cart.append({"name":goods[int(choose) -
1]["name"],"price":goods[int(choose) - 1]["price"],"number":1})
#print(shopping_cart)
print("*" * 50)
else:
print("请输入正确的序号!")
else:
if len(shopping_cart) != 0: #判断购物车是否为空
money_sum = 0
print("我的购物车".center(20,"*"))
print("商品 数量 单价")
for i in shopping_cart: #打印购物车
print(f'{i["name"]} {i["number"]}
{i["price"]}')
money_sum += i["price"] * i["number"] #计算花销
print(f"您的购物车总花费为: {money_sum}")
print(f"您当前的账户余额为:{wallet}.")
if money_sum > wallet: #判断余额是否够while True:#余额不足需要删除购物车的商品直至可以购买为
print("您的余额不足!")
print("我的购物车".center(20, "*"))
print("序号 商品 数量 单价")
for i in range(len(shopping_cart)): #打印购物
车的商品按照序号删除
print(f'{i+1} {shopping_cart[i]
["name"]} {shopping_cart[i]["number"]} {shopping_cart[i]["price"]}')
num_1 = input("请选择您要删除的商品序号")
if num_1.isdecimal() and 0 < int(num_1) <=
len(shopping_cart):
shopping_cart[int(num_1)-1]["number"] -=
1
money_sum -= shopping_cart[int(num_1) -
1]["price"]
if shopping_cart[int(num_1)-1]["number"]
== 0: #如果商品只有一个则直接删除
shopping_cart.pop(int(num_1)-1)
if money_sum <= wallet:
break
else:
print(f"您的购物车总花费为: {money_sum}
")
print(f"您当前的账户余额为:{wallet}.
额不足!")
else:
print("请输入正确的格式!")
if len(shopping_cart) != 0: #结算---
断购物车是否为空
print("购买成功!".center(20,"*"))
print("下面是你购买的物品".center(20))
print("商品 数量 单价")
for i in range(len(shopping_cart)):
print(f'{shopping_cart[i]["name"]}
{shopping_cart[i]["number"]} {shopping_cart[i]["price"]}')
print(f"您此次总消费为: {money_sum}")
wallet -= money_sum #计算余额
print(f"您当前的账户余额为:{wallet}.")
shopping_cart1 += shopping_cart #将此
次的购物信息添加到今天的购物车信息里
else:
print("您的购物车是空的")
else :
print("你的购物车是空的!")
break
else:
print("输入格式有误,请重新选择")
else :
spcart = [ ] #整理后的购物车 包含所有的商品
for i in shopping_cart1:
count2 = 0
for j in range(len(spcart)):
if i["name"] == spcart[j]["name"]:
spcart[j]["number"] += i["number"]count2 = 1
if count2 == 0:
spcart.append(i)
if len(spcart) != 0: #显示所有的购物信息 花销 以及账户余额
gw_money = 0
print("今天购买的所有商品如下".center(20, "*"))
print("序号 商品 数量 单价")
for i in range(len(spcart)):
print(f'{i + 1} {spcart[i]["name"]} {spcart[i]
["number"]} {spcart[i]["price"]}')
gw_money += spcart[i]["price"] * spcart[i]["number"]
print(f"您今天的总花销为{gw_money}")
print(f"您当前的账户余额为:{wallet}.")
else:
print("您今天什么也没买!")
break
else:
print("输入格式有误,请重新选择")

Guess you like

Origin www.cnblogs.com/bky20061005/p/11334057.html