pythonL002学习笔记
学到L002了,来个十六进制的说明吧,学的时候还犯了点错,那十六进制,0123456789ABCDEF,这里是到9就完了,后面接字母了,没有10哦!ABCDEF对应11-16,再来个百度十六进制转二进制的详解,https://jingyan.baidu.com/article/47a29f24292608c0142399cb.html,还不错!
第二课的课堂作业,来个购物车及商品购买的程序:
1 shopping_list = [] 2 product_list = [("IPHONE",5800), 3 ("Mac Pro",12000), 4 ("Coffee",30), 5 ("Bike",800), 6 ("Book",81), 7 ("Alex Python",120),] 8 salary = input("Input your salary:") 9 if salary.isdigit(): 10 salary = int(salary) 11 while True: 12 for index,item in enumerate(product_list): 13 # print(product_list.index(item)+1,item) 通过下标来找 14 print(index,item) 15 user_choice = input(">>>please enter your selected number:") 16 if user_choice.isdigit(): 17 user_choice = int(user_choice) 18 if user_choice<len(product_list) and user_choice>=0: 19 p_item = product_list[user_choice] 20 if p_item[1]<=salary: 21 shopping_list.append(p_item) 22 salary -= p_item[1] 23 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary)) 24 else: 25 print('\033[41;1m你的余额只剩[%s],买了不了\033[0m'%salary) 26 else: 27 print('product code [%s] is not exitst!'%user_choice) 28 elif user_choice == "q": 29 print('--------shopping list--------') 30 for p in shopping_list: 31 print(p) 32 print('your current balance:',salary) 33 print('exit...') 34 exit() 35 else: 36 print('invalid option')
再来个我自己仿写的吧!
1 shopping_cart = [] 2 product_list = [('Iphone',5000), 3 ('Mac Pro',12000), 4 ('Coffee',30), 5 ('Bike',800), 6 ('Alex Python',120), 7 ('Book',81)] 8 salary = input('please Enter your salary:') 9 if salary.isdigit(): 10 salary = int(salary) 11 while True: 12 for index,item in enumerate(product_list): 13 print(index,item) 14 user_choice = input('>>>please enter your selected number:') 15 # isdigit()方法检查字符串是否只包含数字(全由数字组成)。 16 if user_choice.isdigit(): 17 user_choice = int(user_choice) 18 if user_choice<len(product_list) and user_choice>=0: 19 p_item = product_list[user_choice] 20 if p_item[1]<=salary: 21 shopping_cart.append(p_item) 22 salary -= p_item[1] 23 print('您购买的商品是:%s,您的余额为:%s'%(p_item,salary)) 24 else: 25 print('您的余额只剩%s,买不了了'%salary) 26 else: 27 print('您输入的有误!') 28 else: 29 print('请输入数字!')
记录个字符串常用操作:
Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境。
Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。下面例子就是中间显示name,其余用‘-’补充。
1 name = 'my name is sue' 2 print(name.center(50,'-')) 3 4 打印结果: 5 ------------------my name is sue------------------
字典特性:无序的。
第二章的视频我已经看完了,一些知识点也有了初步的认识,我觉的alex老师虽然在课上嘻笑怒骂,但是第三章一开始就给大家上了碗心灵鸡汤,介绍了三本书:《追风筝的人》《白鹿原》《林达看美国》,且不论这三本书怎么样,但其中的正能量我还是能深深的感受到的!
附第二章学习博客地址:http://www.cnblogs.com/alex3714/articles/5717620.html

浙公网安备 33010602011771号