标签:style blog color ar for strong sp div c
>>> list=[‘aaa‘,‘bbb‘,‘ccc‘] >>> print list [‘aaa‘, ‘bbb‘, ‘ccc‘] >>> print list[1] -- list从0开始计数 bbb >>> print list[-1] --输出最后一个 ccc >>> print list[1:3] -- 输出一段列表(顾头不顾尾) [‘bbb‘, ‘ccc‘] >>> list.append(‘ddd‘) -- append添加一个值 >>> print list [‘aaa‘, ‘bbb‘, ‘ccc‘, ‘ddd‘] >>> list.pop() -- pop删除最后一个值 ‘ddd‘ >>> print list [‘aaa‘, ‘bbb‘, ‘ccc‘] >>> list.pop(2) -- 删除某一个位置的值 ‘ccc‘ >>> print list [‘aaa‘, ‘bbb‘] >>> list.count(‘aaa‘) -- count列表中有几个aaa 1 >>> print len(list) --计算列表长度 3 >>> list.index(‘bbb‘) -- index 输出bbb在列表中第几个,没有会报错 1 >>> list[1] -- 第一个确实是bbb ‘bbb‘ >>> list [‘aaa‘, ‘bbb‘, ‘ccc‘] >>> list.insert(2,‘bbb2‘) -- 在2号数前插入一个 >>> list [‘aaa‘, ‘bbb‘, ‘bbb2‘, ‘ccc‘] >>> list[2]=‘bbb3‘ -- 修改一个元素的值 >>> list [‘aaa‘, ‘bbb‘, ‘bbb3‘, ‘ccc‘] >>> del list -- 不用删除列表,以释放内存 >>> test=[‘a‘,‘b‘,‘c‘] -- 用循环打印数组 >>> for i in test: ... print i , ... a b c >>> print range(5) -- 得到一个递增列表 [0, 1, 2, 3, 4] >>> range(3,8) -- 得到一段的递增列表 [3, 4, 5, 6, 7] >>> list=[‘a‘]*10 -- 得到重复的列表 >>> list [‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘a‘]
#!/usr/bin/python salary=int(raw_input(‘Input your salary:‘)) salary_hist=salary whileTrue: shop_list=[‘car 200000‘,‘ipad 4800‘,‘coffee 32‘,‘Mac 8888‘,‘house 2000000‘] for product in shop_list: name =product.split()[0] price=int(product.split()[1]) id=shop_list.index(product) print ‘you can buy: ‘,id,‘ ‘,name,‘ ‘,price max_id=shop_list.index(shop_list[-1]) want_id=raw_input(‘Input the ID you want buy:‘) ############### strong the id input ###################### while want_id ==‘‘ or int(want_id) not in range(0,int(max_id)): print ‘You max input id between 0~%s‘%max_id want_id=raw_input(‘Input the ID you want buy:‘) want_id=int(want_id) ################################################################ name=shop_list[want_id].split()[0] price=int(shop_list[want_id].split()[1]) if price<=salary: salary=salary-price print ‘You have a %s,now you have %s money‘%(name,salary) else: print ‘You have no money,you need to work %s month‘%(price/salary_hist) break
标签:style blog color ar for strong sp div c
原文地址:http://www.cnblogs.com/kissdb/p/4009569.html