今天咱们来作一个购物车的程序联系,首先要理清思路html
那么以上就是这个程序的思路,因为我也是第一次写这个程序,因此若是思路上有什么错误的地方,还请你们谅解,在最后我会放上源码供你们参考,那么如今就开始一块儿写代码叭!!python
1 product_list = [ 2 ('iphone',5000), 3 ('coffee',31), 4 ('bicyle',888), 5 ('iwatch',2666), 6 ('Mac Pro',12000), 7 ('book',88) 8 ]
1 shopping_list = []#空列表,存放购买的商品
1 salary = input("请输入你的工资:") 2 if salary.isdigit():# isdigit() 方法检测字符串是否只由数字组成,是则返回True,不然返回False 3 salary = int(salary)
这里用到了isdigit()方法,在这里稍做解释:git
描述app
Python isdigit() 方法检测字符串是否只由数字组成。框架
语法iphone
isdigit()方法语法:ide
str.isdigit()
参数函数
无学习
返回值网站
若是字符串只包含数字则返回 True 不然返回 False。
实例
如下实例展现了isdigit()方法的实例:
1 #!/usr/bin/python 2 3 str = "123456"; # Only digit in this string 4 print str.isdigit(); 5 6 str = "this is string example....wow!!!"; 7 print str.isdigit();
输出结果为:
True
False
也就是说,若是你的字符串输入的是数字,那么返回的是True,正如我写的代码,用if来判断,若是个人rmb是数字,那么成立,能够继续循环,这里你们应该都能理解,固然,若是不是,代码为:
1 else: 2 print("输入错误")
固然,我总体的代码是一个大的框架,先要判断我输入的rmb是否为数字,若是是,那么将其转为int类型,若是不是则输出:“输入错误”而后再将其余代码写入个人这个大的框架中,写程序要有一个大的框架,在脑子里先定了型,这样写起来比较调理
这个循环用到了 while True: 这样我就能够无限的去购买我心仪的物品,在其中添加一些其余的代码来丰富程序
先放一下其中的代码:
1 while True: 2 for index,i in enumerate(product_list):#index做为下标索引 3 print(index,i) 4 #enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,通常用在 for 循环当中。 5 user_choice = input("请输入你要购买的商品:") 6 if user_choice.isdigit(): 7 user_choice = int(user_choice) 8 if user_choice < len(product_list) and user_choice >= 0: 9 product_choice = product_list[user_choice] 10 if product_choice[1] < salary:#买得起 11 shopping_list.append(product_choice)#买得起,就放入购物车 12 salary -= product_choice[1] 13 print("Add %s into your shopping_list,your balance is \033[31;1m%s\033[0m"%(product_choice,salary)) 14 else: 15 print("\033[41;1m你的余额只剩%s啦,还买个叼啊!\033[0m"%salary) 16 print("---------shopping list-----------") 17 for s_index, s in enumerate(shopping_list): 18 print(s_index, s) 19 print("---------shopping list-----------") 20 print("你的余额为:\033[31;1m%s\033[0m" % salary) 21 else: 22 print("没有这个商品") 23 elif user_choice == "q": 24 print("---------shopping list-----------") 25 for s_index,s in enumerate(shopping_list): 26 print(s_index,s) 27 print("---------shopping list-----------") 28 print("你的余额为:\033[31;1m%s\033[0m"%salary) 29 exit() 30 else: 31 print("输入错误")
1 for index,i in enumerate(product_list):#index做为下标索引 2 print(index,i)
这里用到了enumerate方法,在这里稍做解释:
描述
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,通常用在 for 循环当中。
for 循环使用 enumerate
就是将列表中的值前方加上了数据下标,上面的代码输出结果为:
0 ('iphone', 5000) 1 ('coffee', 31) 2 ('bicyle', 888) 3 ('iwatch', 2666) 4 ('Mac Pro', 12000) 5 ('book', 88)
针对程序中所用到的一些对于像我同样的初学者来讲,能够参考 http://www.runoob.com/python/python-tutorial.html 这个网站里的一些知识点来进行学习
1 user_choice = input("请输入你要购买的商品:") 2 if user_choice.isdigit(): 3 user_choice = int(user_choice)
这三行很好理解,与输入rmb那块的代码段相似,第二第三行一样是将判断是不是数字,若是是,转成int型,不然 else: print("没有这个商品") ,这个很好理解,运用循环的时候,有 if 就有else,这是一个
固有的流程,咱们写程序时,头脑中要有思路,善始善终
1 if user_choice < len(product_list) and user_choice >= 0: 2 product_choice = product_list[user_choice] 3 if product_choice[1] < salary:#买得起 4 shopping_list.append(product_choice)#买得起,就放入购物车 5 salary -= product_choice[1] 6 print("Add %s into your shopping_list,your balance is \033[31;1m%s\033[0m"%(product_choice,salary)) 7 else: 8 print("\033[41;1m你的余额只剩%s啦,还买个叼啊!\033[0m"%salary) 9 else: 10 print("没有这个商品")
这段代码比较长,但也相对简单,我来解释一下
1 if user_choice < len(product_list) and user_choice >= 0: 2 product_choice = product_list[user_choice]
首先是要判断,用户所选择的数字,是否小于商品的编号,而且大于等于0,在前面我也将每一个商品所对应的索引值打印了下来,利用这个来判断咱们是否选择了商品;接着定义了 product_choice 这个就是咱们选择加入购物车的商品,它的值等于 product_list[user_choice] ,这段的意思是:用户输入要购买商品的编号,这个数字传给 product_list[user_choice] ,就将商品选择到了,举个栗子,譬如用户选择 user_choice 为 1 ,那么咱们的 product_list[user_choice] 就变成了 product_list[1] ,这也就是咱们将第二个商品选择到了,而后将这个值赋予给了 product_choice ,这就是这段代码所表达的含义,可能表达的比较啰嗦,大神轻喷
1 else: 2 print("没有这个商品")
那么若是这个值在范围之外,那么就输出 “没有这个商品”
1 if product_choice[1] < salary:#买得起 2 shopping_list.append(product_choice)#买得起,就放入购物车 3 salary -= product_choice[1] 4 print("Add %s into your shopping_list,your balance is \033[31;1m%s\033[0m"%(product_choice,salary)) 5 else: 6 print("\033[41;1m你的余额只剩%s啦,还买个叼啊!\033[0m"%salary)
而后就要判断我是否买得起这件商品,首先要明白一个知识,个人商品列表中的 [(),()]第一个值是物品名称第二个值是价格,对应到计算机中就是 0 , 1 这两个索引 ,那么就进入咱们的判断:
1 if product_choice[1] < salary:#买得起 2 shopping_list.append(product_choice)#买得起,就放入购物车 3 salary -= product_choice[1]
product_choice[1] 这个表明的是我所选商品的第二个值,即价格,若是小于个人rmb,说明买得起,那么用到了列表中的 append()方法:
描述
append() 方法用于在列表末尾添加新的对象。
语法
append()方法语法:
1 list.append(obj)
用这个方法,将咱们所选商品加入到咱们的购物车列表当中,此时,咱们的购物车列表就有了第一个商品(以前购物车列表定义的是空列表)
1 salary -= product_choice[1]
而后,rmb须要减去咱们购买的价格
1 print("Add %s into your shopping_list,your balance is \033[31;1m%s\033[0m"%(product_choice,salary))
而后输出:咱们已经将商品加入到了购物车内,你的余额为xx
1 \033[31;1m%s\033[0m
这个是控制高亮显示的代码,须要死记硬背,没有捷径,多写代码天然就记住了
1 else: 2 print("\033[41;1m你的余额只剩%s啦,还买个叼啊!\033[0m"%salary)
若是商品价格大于rmb那么就输出余额不足
1 print("---------shopping list-----------") 2 for s_index, s in enumerate(shopping_list): 3 print(s_index, s) 4 print("---------shopping list-----------") 5 print("你的余额为:\033[31;1m%s\033[0m" % salary)
余额不足以后呢,我须要输出你购物车内的商品,而后将购物车商品输出,而后再跳回继续选择商品;这个很好理解,我用 for 循环将购物车商品输出 ,以上几段代码是控制购买商品的代码,相对来讲比较核心,须要仔细品味
1 elif user_choice == "q": 2 print("---------shopping list-----------") 3 for s_index,s in enumerate(shopping_list): 4 print(s_index,s) 5 print("---------shopping list-----------") 6 print("你的余额为:\033[31;1m%s\033[0m"%salary) 7 exit()
这个代码段是创建在 用户选择 if user_choice.isdigit(): 这个基础上的, 也就是 user_choice = input("请输入你要购买的商品:") 我输入了商品前的编号, 而后进入购买商品的循环, 若是咱们以为够了,不想再买了,选择 “ q ” 而后就能够打印购买商品,各回各家各找各妈了~!
#!/usr/bin/env.python # -*- coding: utf-8 -*- """ ------------------------------------------------- File Name: shopping Description : Author : lym date: 2018/2/24 ------------------------------------------------- Change Activity: 2018/2/24: ------------------------------------------------- """ __author__ = 'lym' #定义一个商品列表,里面写入商品的值和价格 product_list = [ ('iphone',5000), ('coffee',31), ('bicyle',888), ('iwatch',2666), ('Mac Pro',12000), ('book',88) ] shopping_list = []#空列表,存放购买的商品 salary = input("请输入你的工资:") if salary.isdigit():# isdigit() 方法检测字符串是否只由数字组成,是则返回True,不然返回False salary = int(salary) while True: for index,i in enumerate(product_list):#index做为下标索引 print(index,i) #enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,通常用在 for 循环当中。 user_choice = input("请输入你要购买的商品:") if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(product_list) and user_choice >= 0: product_choice = product_list[user_choice] if product_choice[1] < salary:#买得起 shopping_list.append(product_choice)#买得起,就放入购物车 salary -= product_choice[1] print("Add %s into your shopping_list,your balance is \033[31;1m%s\033[0m"%(product_choice,salary)) else: print("\033[41;1m你的余额只剩%s啦,还买个叼啊!\033[0m"%salary) print("---------shopping list-----------") for s_index, s in enumerate(shopping_list): print(s_index, s) print("---------shopping list-----------") print("你的余额为:\033[31;1m%s\033[0m" % salary) else: print("没有这个商品") elif user_choice == "q": print("---------shopping list-----------") for s_index,s in enumerate(shopping_list): print(s_index,s) print("---------shopping list-----------") print("你的余额为:\033[31;1m%s\033[0m"%salary) exit() else: print("输入错误") else: print("输入错误")