Day2:
-
Python 有丰富的标准库(不需要下载,直接导入)与第三方库(下载安装,放在SITE-packages中);学习标准库:SYS模块(打印环境变量)
-
Python是什么?Python与Java/c#一样,是一门基于虚拟机的语言,先编译后解释(运行),内存中有一个Pycodeobject,该文件是python编译器真正编译成的结果,即python程序运行时,编译结果则保存在位于内存中的pycodeobject中,当Python运行结束时,Python解释器则将Pycodeobject写回Pyc文件中。pyc文件就是Pycodeobject的一种持久化保存方式。
-
数据类型:int(整形);long(长整形);float(浮点型);complex(复数);布尔值(0/1);字符串。
-
三元运算:result = 值1 if 条件 else 值2
-
进制:二进制(0、1)、八进制(0-7)、十进制(0-9)、十六进制(0-F),二进制与字符串转换用encode与decode(编码/解码)
-
列表的增删改查,主要用[ ]; 元组只可查,用()表示。
-
运用循环与元组编写购物车程序。
-
product_list = [ ('Iphone',5800), ('Mac pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Python book',120) ] shopping_list=[] salary = input("Input your salary:") #输入工资 if salary.isdigit(): #判断工资是否为数字 salary = int (salary) #整数化 while True: #进入死循环 for index,item in enumerate(product_list): #取出元组下标为商品序号 #print(product_list.index(item),item print(index,item) #打印商品列表 user_choice = input("选择需要购买物品货号>>>:") if user_choice.isdigit(): #判断是否为数字 user_choice = int(user_choice) if user_choice < len(product_list) and user_choice >= 0: #输入有效商品序号 p_item = product_list[user_choice] #通过下表取出商品 if p_item[1] <= salary: # 判断是否买得起 shopping_list.append(p_item) #添加到商品列表中 salary -= p_item[1] #扣钱 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % (p_item, salary)) #\033[31;1m%s\033[0m 表示标注为红色;31改为32,为绿色 else: print("\033[41;1m你的余额不足!还有[%s]\033[0m" %salary) else: print("product code [%s] is not exist!"%user_choice) #商品序号不存在 elif user_choice == 'q': #推出 print("------------shopping list----------") for p in shopping_list: print(p) print("your current balance:",salary) exit() else: print("invalid option")