Python第二次写的代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python第二次写的代码相关的知识,希望对你有一定的参考价值。
#!/bin/bash/env python # -*- coding:utf-8 -*- #function:将大于66值保存在第一个字典键中,小于66保存在第二个 i1 = [11,22,33,44,55,66,77,88,99,90] dict = { ‘k1‘: [], ‘k2‘: [], } for i in i1: if i <= 66: dict[‘k1‘].append(i) else: dict[‘k2‘].append(i) print(dict)
结果:
{‘k1‘: [11, 22, 33, 44, 55, 66], ‘k2‘: [77, 88, 99, 90]}
注意.append的用法,就是将i值添加到k1键中。还有数字不用‘‘号。并且注意字典的格式,每次定义键的时候都需要加上,号。
#!/bin/bash/env python # -*- coding:utf-8 -*- #function:输出商品列表,用户输入序号,显示用户选中商品 i1 = [‘shouji‘,‘diannao‘,‘shubiao‘,‘youting‘] for key,item in enumerate(i1): print(key,item) inp = input("Please enter market number:") inp_number = int(inp) print(i1[inp_number])
#!/bin/bash/env python # -*- coding:utf-8 -*- #function:简单的购物车 inp1 = input("请输入您的总资产:") inp1_number = int(inp1) print(‘You have‘,inp1_number,‘dollars‘) goods = [ {"name": "dianano","price": 1999}, {"name": "shubiao",‘price‘: 10}, {"name": ‘youting‘,‘price‘: 20}, {"name": "shouji","price": 1550}, ] for key,item in enumerate(goods): print(key,item) total = 0 while True: inp2 = input("Please enter the product number:") inp2_number = int(inp2) print(goods[inp2_number],‘has join in the shopping cart‘) total1 = goods[inp2_number][‘price‘] total = total + total1 print(total) inp3 = input("如果你不想买了,请输Y/y:") i4 = inp3.lower() if i4 == ‘y‘: break if inp1_number >= total: print(‘OK,you have buy it‘) else: print(‘NO,you cant afford it‘)
以上是关于Python第二次写的代码的主要内容,如果未能解决你的问题,请参考以下文章