數據結構:
goods = [ {"name": "電腦", "price": 1999}, {"name": "鼠標", "price": 10}, {"name": "遊艇", "price": 20}, {"name": "美女", "price": 998}, ...... ] 功能要求: 基礎要求: 1、啓動程序後,輸入用戶名密碼後,讓用戶輸入工資,而後打印商品列表 2、容許用戶根據商品編號購買商品 3、用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 4、可隨時退出,退出時,打印已購買商品和餘額 5、在用戶使用過程當中, 關鍵輸出,如餘額,商品已加入購物車等消息,需高亮顯示 擴展需求: 1、用戶下一次登陸後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的餘額什麼的仍是那些,再次登陸可繼續購買 二、容許查詢以前的消費記錄
# -*- 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('你輸入的有誤,請從新輸入')
#-*- 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")
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 | 白色 -------------------------------------------
# -*- 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")
# -*- 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")