2 -15 購物車程序
阿新 • • 發佈:2019-02-11
this 購物 輸出 一次 records gif 八進制 product tin
1.需求
數據結構:
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998},
......
]
功能要求:
基礎要求:
1、啟動程序後,輸入用戶名密碼後,讓用戶輸入工資,然後打印商品列表
2、允許用戶根據商品編號購買商品
3、用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
4、可隨時退出,退出時,打印已購買商品和余額
5、在用戶使用過程中, 關鍵輸出,如余額,商品已加入購物車等消息,需高亮顯示
擴展需求:
1、用戶下一次登錄後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的余額什麽的還是那些,再次登錄可繼續購買
2、允許查詢之前的消費記錄
2.普通流程圖
# -*- coding:utf-8 -*-
product = [[‘Iphone8‘, 6888], [‘MacPro‘, 14800], [‘小米6‘, 2499], [‘Coffee‘, 31], [‘Book‘, 80], [‘Nike Shoes‘, 799]]
shopping_cart = []
flag = False # 標誌位
while not flag:
print("----------商品列表 --------")
for index, item in enumerate(product):
msg = "%s. %s %s" % (index, item[0], item[-1])
print(msg)
choice = input("輸入你要買的商品編號|退出q :")
if choice.isdigit():
choice = int(choice)
if choice < len(product):
shopping_cart.append(product[choice])
print(‘-----你購買了‘,product[choice])
else:
print(‘你輸入的商品不存在‘)
elif choice == ‘q‘:
if len(shopping_cart) > 0:
print("------你的購物車---------")
for index, item in enumerate(shopping_cart):
msg = "%s. %s %s" % (index, item[0], item[-1])
print(msg)
flag = True
# break
else:
print(‘你輸入的有誤,請重新輸入‘)
3.流程圖
4.基本需求版本
#-*- coding:utf-8 -*-
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998}
]
shopping_cart = []
_username = ‘alex‘
_password = ‘123‘
while True: # 用戶名密碼循環1
username = input("請您輸入用戶名:").strip()
password = input("請您輸入密碼:").strip()
if username == _username and password == _password:
print("\033[1;34m-----登錄成功,歡迎%s\033[0m"%username)
while True: # 工資循環2
salary = input("請輸入你的工資:").strip()
if not salary:
continue
if salary.isdigit():
salary = int(salary)
while True: # 商品列表循環3
print("----------商品列表 --------")
for index, item in enumerate(goods):
print("%s %s %s" % (index, item[‘name‘], item[‘price‘]))
choice = input("輸入你要買的商品編號|退出q :").strip()
if not choice:
continue
if choice.isdigit():
choice = int(choice)
if choice < len(goods):
if salary >= goods[choice][‘price‘]:
shopping_cart.append([goods[choice][‘name‘], goods[choice][‘price‘]]) # 加入商品名,price
print(‘\033[1;32m>你購買了%s\033[0m‘%goods[choice][‘name‘])
salary -= goods[choice][‘price‘]
print(‘\033[1;31m>余額剩余%s\033[0m‘%salary)
else:
print("\033[1;31m余額不足,請重新選擇\033[0m")
else:
print(‘\033[1;31;47m你輸入的商品不存在\033[0m‘)
elif choice == ‘q‘:
if len(shopping_cart) > 0:
print("\033[1;34m------你的購物車---------")
for index, item in enumerate(shopping_cart):
print(index, item[0], item[-1])
print("------------------------")
print("你的余額:%s\033[0m"%salary)
exit()
else:
print("\033[1;34;47m你的購物車為空,你的余額:%s\033[0m"%salary)
exit()
else:
print(‘\033[1;31;47m你輸入的有誤,請重新輸入\033[0m‘)
else:
print(‘\033[1;31m你輸入的有誤,請重新輸入\033[0m‘)
else:
print("\033[1;31;47m用戶名或密碼錯誤\033[0m")
5.高亮顯示
我們可以通過對有用的信息設置不同顏色來達到醒目的效果,因為我平時都是在linux下開發,
而linux終端中的顏色是用轉義序列控制的,轉義序列是以ESC開頭,可以用\033完成相同的工作(ESC的ASCII碼用十進制表示就是27,等於用八進制表示的33)。
顯示顏色格式:\033[顯示方式;字體色;背景色m......[\033[0m
print(‘This is a \033[1;35m test \033[0m!‘)
print(‘This is a \033[1;32;43m test \033[0m!‘)
print(‘\033[1;33;44mThis is a test !\033[0m‘)
-------------------------------
顯示方式 | 效果
-------------------------------
0 | 終端默認設置
1 | 高亮顯示
4 | 使用下劃線
5 | 閃爍
7 | 反白顯示
8 | 不可見
-------------------------------
-------------------------------------------
-------------------------------------------
字體色 | 背景色 | 顏色描述
-------------------------------------------
30 | 40 | 黑色
31 | 41 | 紅色
32 | 42 | 綠色
33 | 43 | 黃色
34 | 44 | 藍色
35 | 45 | 紫紅色
36 | 46 | 青藍色
37 | 47 | 白色
-------------------------------------------
6.擴展需求
# -*- coding:utf-8 -*-
import time
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998}
]
shopping_cart = []
_username = ‘alex‘
_password = ‘123‘
while True: # 用戶名密碼循環1
username = input("\033[1;32m請您輸入用戶名:\033[0m").strip()
password = input("\033[1;32m請您輸入密碼:\033[0m").strip()
if username == _username and password == _password:
print("\033[1;34m-----登錄成功,歡迎%s\033[0m"%username)
while True: # 工資循環2
with open(‘salary‘, ‘r‘) as f1:
salary = f1.read()
if salary:
print(‘\033[1;31m你的余額還有:%s\033[0m‘ % salary)
else:
salary = input("\033[1;32m請輸入你的工資:\033[0m").strip()
if not salary:
continue
if salary.isdigit():
salary = int(salary)
with open(‘salary‘, ‘w‘) as f2:
f2.write(str(salary))
while True: # 商品列表循環3
print("----------商品列表 --------")
for index, item in enumerate(goods):
print("%s %s %s" % (index, item[‘name‘], item[‘price‘]))
choice = input("\033[1;34m輸入你要買的商品編號|查看消費記錄b|退出q:\033[0m").strip()
if not choice:
continue
if choice.isdigit():
choice = int(choice)
if choice < len(goods):
if salary >= goods[choice][‘price‘]:
shopping_cart.append([goods[choice][‘name‘], goods[choice][‘price‘]])
# 消費記錄加入文件
with open(‘shopping_records‘, ‘a‘) as f:
now_time = time.ctime()
goods_choice = [goods[choice][‘name‘], goods[choice][‘price‘]]
record = str(now_time) + ‘\t‘ + str(goods_choice) + ‘\n‘
f.write(record)
print(‘\033[1;32m>你購買了%s\033[0m‘%goods[choice][‘name‘])
salary -= goods[choice][‘price‘]
print(‘\033[1;31m>余額剩余%s\033[0m‘%salary)
else:
print("\033[1;31m余額不足,請重新選擇\033[0m")
else:
print(‘\033[1;31m你輸入的商品不存在\033[0m‘)
elif choice == ‘b‘:
with open(‘shopping_records‘, ‘r‘) as f:
records = f.read()
if len(records):
print(‘-------消費記錄------‘)
print(records)
else:
print(‘\033[1;31m>>你還沒有買過東西\033[0m‘)
elif choice == ‘q‘:
if len(shopping_cart) > 0:
print("\033[1;32m------你的購物車---------")
for index, item in enumerate(shopping_cart):
print(index, item[0], item[-1])
print("------------------------")
print("你的余額:%s\033[0m"%salary)
with open(‘salary‘, ‘w‘) as f2:
f2.write(str(salary))
exit()
else:
print("\033[1;31m你的購物車為空,你的余額:%s\033[0m"%salary)
exit()
else:
print(‘\033[1;31;47m你輸入的有誤,請重新輸入\033[0m‘)
else:
print(‘\033[1;31m你輸入的有誤,請重新輸入\033[0m‘)
else:
print("\033[1;31;47m用戶名或密碼錯誤\033[0m")
8.修改後
# -*- coding:utf-8 -*-
import time
goods = [
{"name": "電腦", "price": 1999},
{"name": "鼠標", "price": 10},
{"name": "遊艇", "price": 20},
{"name": "美女", "price": 998}
]
shopping_cart = []
_username = ‘alex‘
_password = ‘123‘
count = 0
while count<3 : # 用戶名密碼循環1
username = input("\033[1;32m請您輸入用戶名:\033[0m").strip()
password = input("\033[1;32m請您輸入密碼:\033[0m").strip()
if username == _username and password == _password:
print("\033[1;34m-----登錄成功,歡迎%s\033[0m"%username)
# 得到工資
with open(‘salary‘, ‘r‘) as f1:
data = f1.read()
if data:
salary = float(data)
print(‘\033[1;31m你的余額還有:%s\033[0m‘ % salary)
else:
while True: # 工資循環2
salary = input("\033[1;32m請輸入你的工資:\033[0m").strip()
if salary.isdigit(): # 只能夠把 3456轉換,不能轉換 3456.444,
salary = float(salary)
break
else:
print(‘你輸入的有誤,請重新輸入‘)
while True: # 商品列表循環3
print("----------商品列表 --------")
for index, item in enumerate(goods):
print("%s %s %s" % (index, item[‘name‘], item[‘price‘]))
choice = input("\033[1;34m輸入你要買的商品編號|查看消費記錄b|退出q:\033[0m").strip()
if choice.isdigit():
choice = int(choice)
if choice < len(goods):
if salary >= float(goods[choice][‘price‘]):
shopping_cart.append([goods[choice][‘name‘], goods[choice][‘price‘]])
# 消費記錄加入文件
with open(‘shopping_records‘, ‘a‘) as f:
now_time = time.ctime()
goods_choice = [goods[choice][‘name‘], goods[choice][‘price‘]]
record = str(now_time) + ‘\t‘ + str(goods_choice) + ‘\n‘
f.write(record)
print(‘\033[1;32m>你購買了%s\033[0m‘%goods[choice][‘name‘])
salary -= float(goods[choice][‘price‘])
print(‘\033[1;31m>余額剩余%s\033[0m‘%salary)
else:
print("\033[1;31m余額不足,請重新選擇\033[0m")
else:
print(‘\033[1;31m你輸入的商品不存在\033[0m‘)
elif choice == ‘b‘:
with open(‘shopping_records‘, ‘r‘) as f:
records = f.read()
if len(records):
print(‘-------消費記錄------‘)
print(records)
else:
print(‘\033[1;31m>>你還沒有買過東西\033[0m‘)
elif choice == ‘q‘:
if len(shopping_cart) > 0:
print("\033[1;32m------你的購物車---------")
for index, item in enumerate(shopping_cart):
print(index, item[0], item[-1])
print("------------------------")
print("你的余額:%s\033[0m"%salary)
with open(‘salary‘, ‘w‘) as f2:
f2.write(str(salary))
exit()
else:
print("\033[1;31m你的購物車為空,你的余額:%s\033[0m"%salary)
with open(‘salary‘, ‘w‘) as f2:
f2.write(str(salary))
exit()
else:
print(‘\033[1;31;47m你輸入的有誤,請重新輸入\033[0m‘)
else:
print("\033[1;31;47m用戶名或密碼錯誤\033[0m")
count += 1
print("you have try more times")
2 -15 購物車程序