No.003-Python-学习之路-Day2-pyc|color|三元运算|mod初识|Task_ShoppingCart
PYC
“.pyc”文件是python执行前进行的简单编译的保存文件
python文件运行过程:
第一次执行:
python会先进行简单编译并把编译结果(pyCodeObject)存在内存中 ====> 用python解释器解释pyCodeObject ====> 执行结束后,将pyCodeObject放入“.pyc”文件中;
第二次执行;
python会去找“.pyc”文件 ====> 找到后比较“.pyc”及“.py”文件的更新时间 ====> “.pyc”较新则直接解释并执行 ====> 过程中任何不成立,则同第一次执行;
Color
高亮显示方法:
开头部分: \033[显示方式;前景色;背景色m + 结尾部分: \033 [0m
显示方式: 0-默认值 1-高亮 22-非粗体 4-下划线 24-非下划线 5-闪烁 25-非闪烁 7-反显 27-非反显
前景色 : 30-黑色 31-红色 32-绿色 33-黄色 34-蓝色 35-洋红 36-青色 37-白色
背景色 : 40-黑色 41-红色 42-绿色 43-黄色 44-蓝色 45-洋红 46-青色 47-白色
print("\033[0mHello World!!!") #若没有尾部,则自此默认的输出变为设置中的样子 print("\033[0:31mHello World!!!\033[0m") print("Hello World!!!") print("\033[1:31mHello World!!!\033[0m") print("\033[4:34:41mHello World!!\033[0m")
三元运算
#三元运算 a, b, c = 1, 3, 5 d = a if a > b else c #等同于 if a > b: d = a else d = c
MOD初识
OS mod(Operate System)
Python标准库中的一个用于访问操作系统功能的模块。
使用OS模块中提供的接口,可以实现跨平台访问。
通用操作:
1、获取平台信息
2、对目录的操作
3、判断操作
import os # method-1 # 执行命令结果打印至屏幕,返回执行成功还是失败 cmd_res = os.system("dir") print("----->", cmd_res) # 输入内存地址(内容存在的位置) cmd_res = os.popen("dir") print("----->", cmd_res) # 需要去该内存中去取 cmd_res = os.popen("dir").read() print("----->", cmd_res) #新建目录 os.mkdir("new_dir")
#目录操作 print("获取目录下文件名:%s" % os.listdir(os.getcwd())) os.rmdir("test_rename") #多级删除 os.removedirs() os.mkdir("test_mkdir") #多级创建 os.makedirs() os.chdir("test_mkdir") print("删-增-变目录后:%s" % os.getcwd()) os.chdir("..") os.rename("test_mkdir", "test_rename") os.chdir("test_rename") print("重命名目录后:%s" % os.getcwd())
Task-1:Shopping Cart
需求:
1.商家(添加,删除,修改商品)
2.用户(充值,购买商品)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/11/13 20:27 # @Author : Bruce Lee # Input Your Money class store(object): def __init__(self, name, price, goods_file): self.name = name self.price = price self.goods_file = goods_file self.single_good = "" # 载入商品文件 self.gd = open(goods_file, "r", encoding="utf-8") self.goods = self.gd.readlines() self.gd.close() self.index = "" self.find_result = "" def show_goods(self): for self.index, self.single_good in enumerate(self.goods): self.single_good = self.single_good.strip().split(",") print("商品{index}\t{name}{price}元".format( index=str(self.index + 1).zfill(3), name=self.single_good[0].ljust(6, " "), price=self.single_good[1].rjust(5, " ") ).expandtabs(10)) def add_goods(self): for self.single_good in self.goods: self.find_result = self.single_good.find(self.name) if self.find_result >= 0: break if self.find_result == -1: self.goods.append("{name},{price}\n".format(name=self.name, price=self.price)) self.gd = open(goods_file, "w", encoding="utf-8") self.gd.writelines(self.goods) self.gd.close() print(self.goods) else: print("已有商品请忽重复添加!!") def del_goods(self): for self.single_good in self.goods: self.find_result = self.single_good.find(self.name) if self.find_result == -1: continue else: self.goods.remove(self.single_good) self.gd = open(goods_file, "w", encoding="utf-8") self.gd.writelines(self.goods) self.gd.close() print("已删除商品-%s" % self.name) break def edit_goods(self): for self.index, self.single_good in enumerate(self.goods): self.find_result = self.single_good.find(self.name) if self.find_result == -1: continue else: self.goods[self.index] = "{name},{price}\n".format( name=self.name, price=self.price ) self.gd = open(goods_file, "w", encoding="utf-8") self.gd.writelines(self.goods) self.gd.close() print("已修改商品[%s]的价格为%s元" % (self.name, self.price)) break class customer(object): def __init__(self, name, balance, customers_file): self.customer_name = name self.ct = open(customers_file, "r", encoding="utf-8") self.customers = self.ct.readlines() self.ct.close() self.find_result = "" self.customers_list = [] self.balance = balance self.recharge = "" for self.customer in self.customers: self.customer = self.customer.strip().split(",") self.customers_list.append(self.customer) def show_customers(self): for self.customer in self.customers_list: print("用户[%s]" % self.customer[0]) def get_balance(self): for self.customer in self.customers_list: if self.customer[0] == self.customer_name: self.balance = self.customer[1] break else: self.balance = "-1" return self.balance def recharge1(self): while True: self.recharge = input("请输出您充值的金额:") if self.recharge.isdigit(): self.recharge = int(self.recharge) break elif self.recharge == "": print("输入不可为空!!") else: print("您的输入有误,请重新输入!!") for self.single_customer_index, self.single_customer in enumerate(self.customers): self.find_result = self.single_customer.find(self.customer_name) if self.find_result == -1: continue else: self.balance = 0 self.single_customer = self.single_customer.strip().split(",") self.balance = int(self.single_customer[1]) self.balance += self.recharge self.single_customer[1] = str(self.balance) self.customers[self.single_customer_index] = ",".join(self.single_customer) + "\n" self.ct = open(customers_file, "w", encoding="utf-8") self.ct.writelines(self.customers) self.ct.close() break print("用户名[%s]已充值%d元" % (self.customer_name, self.recharge)) def after_shopping(self): for self.single_customer_index, self.single_customer in enumerate(self.customers): self.find_result = self.single_customer.find(self.customer_name) if self.find_result == -1: continue else: self.customers[self.single_customer_index] = "{name},{balance}\n".format( name=self.customer_name, balance=self.balance ) self.ct = open(customers_file, "w", encoding="utf-8") self.ct.writelines(self.customers) self.ct.close() print("用户名[%s]的账户余额为%s元" % (self.customer_name, self.balance)) goods_file = "D:\\资料存放\\PythonObjects\\day2\\goods_list.txt" customers_file = "D:\\资料存放\\PythonObjects\\day2\\customers.txt" spend = 0 while True: choice = input("角色列表:\n1.商家\n2.客户\n3.退出\n您的选择是:") if choice == "1": print("进入商家操作模式\n") while True: choice_store = input("商家操作列表:\n1.展示\n2.添加\n3.删除\n4.编辑\n5.退出\n您的选择是:") if choice_store == "1": store_show = store("", "", goods_file) store_show.show_goods() continue elif choice_store == "2": name = input("添加商品的名称:") price = input("添加商品的价格:") store_add = store(name, price, goods_file) store_add.add_goods() continue elif choice_store == "3": name = input("删除商品的名称:") store_del = store(name, "", goods_file) store_del.del_goods() continue elif choice_store == "4": name = input("修改商品的名称:") price = input("商品重新定价为:") store_edit = store(name, price, goods_file) store_edit.edit_goods() continue elif choice_store == "5": print("已退出商家模式\n") break else: print("输入有误,请从新输入\n") continue elif choice == "2": print("进入客户操作模式\n") customer_balance = "" customer_name = "" goods_list = [] shopping_list = [] gd = open(goods_file, "r", encoding="utf-8") goods = gd.readlines() gd.close() for singe_goods in goods: goods_list.append(singe_goods.strip().split(",")) print(goods_list) # 选择用户 while True: customer_show = customer("", "", customers_file) customer_show.show_customers() customer_name = input("您选择哪个用户:") customer_get_balance = customer(customer_name, "", customers_file) if customer_get_balance.get_balance() == "-1": print("用户输入错误,请重新输入!") continue else: customer_balance = customer_get_balance.get_balance() print("您的余额是[{0}]".format(customer_balance)) choice_recharge = input("是否充值y/n:") if choice_recharge == "y" or choice_recharge == "Y": customer_recharge = customer(customer_name, "", customers_file) customer_recharge.recharge1() customer_get_balance = customer(customer_name, "", customers_file) customer_balance = customer_get_balance.get_balance() customer_balance = int(customer_balance) break while True: print("商品列表如下:") store_show = store("", "", goods_file) store_show.show_goods() print("结账0") choice_customer = input("输入您要购买的商品:") if not choice_customer.isdigit(): print("请输入正确的商品编号:") continue elif choice_customer == "0": new_balance = customer_balance-spend customer_settle = customer(customer_name, new_balance, customers_file) customer_settle.after_shopping() if not shopping_list: print("购物车为空,未购买任何商品!") print("已结账,您一共花费{spend}元,您的余额为{balance}元!".format( spend=spend, balance=new_balance )) else: print("-----您购买的商品如下:-----") for buying_goods_index, buying_goods in enumerate(shopping_list): print("商品{index}\t{goods}\t{price}".format( index=buying_goods_index, goods=buying_goods[0], price=buying_goods[1] ).expandtabs(20)) print("已结账,您一共花费{spend}元,您的余额为{balance}元!".format( spend=spend, balance=new_balance )) break elif int(choice_customer) > len(goods_list): print("您输入的商品编号过大!") continue else: shopping_list.append(goods_list[int(choice_customer) - 1]) print("已添加商品[%s]" % goods_list[int(choice_customer) - 1][0]) spend = 0 for item in shopping_list: spend += int(item[1]) if spend > customer_balance: del_item = shopping_list.pop() spend -= int(del_item[1]) print("您的余额不足了!!删除最新加入商品[%s]" % del_item[0]) continue print("-----您的余额为[{0}]-----".format(customer_balance-spend)) elif choice == "3": print("已退出!!") break else: print("输入有误!!")

浙公网安备 33010602011771号