python下 list/dictionary

一、list

实例 1:

-------------------------------shop_list.py----------------------------------
#!/usr/bin/python
#coding=utf-8
import tab

products = ['car','iphone','coffee','mac','mouse','bicyle']
prices   = [25000, 4999,    35,     9688,  332,      1500]
shop_list = []

salary = int(raw_input('please input your salary:'))

while True:
    print "things have in the shop,please choose one to buy:"
    for p in products:
        print p,'\t',prices[products.index(p)]

    choice = raw_input('please input one item to buy:')
    f_choice = choice.strip()
    if f_choice in products:
        product_price_index = products.index(f_choice)
        product_price = prices[product_price_index]
        print f_choice,product_price
        if salary > product_price:
            shop_list.append(f_choice)
            print "added %s into your shop list" % f_choice
            salary = salary - product_price
            print "Salary left:",salary
        else:
            if salary < min(prices):
                print "sorry,rest of your salary cannot buy anything!"
                print "you have bought these things:%s" % shop_list
            else:
                print "you cannot afford this product,try other ones!"

实例 2

-------------------------------shops.txt-------------------------------------
car      25000
iphone   4999
coffee   35
mac      9688
bicyle   432

-------------------------------shop2_list.py---------------------------------
#!/usr/bin/python
#coding=utf-8
import tab
products = []
prices = []
shop_list = []

#----------------------把产品、价格加入到系统中

f = file('shops.txt')
for line in f.readlines():
    new_line = line.split()
    products.append(new_line[0])
    prices.append(int(new_line[1]))
#print products
#print prices

salary = int(raw_input("please input your salary:"))

#此脚本是让用户不停的买东西,直到木有钱为止,因此用while循环
while True:
    print "welcome,things you can buy as below"

#----------------------打印产品、价格菜单
    for p in products:
        p_index = products.index(p)
        p_price = prices[p_index]
        print p,'\t',p_price

    choice = raw_input("please input what you want to buy:")
    f_choice = choice.strip()
    if f_choice in products:
        print "\033[31;1myes,it is in the list\033[0m"
        f_index = products.index(f_choice)
        f_price = prices[f_index]
        if salary >= f_price:
            print "\033[34;1mCongratulations,added %s to shop list\033[0m" %f_choice
            shop_list.append(f_choice)
            salary = salary - f_price
            print "Now you have %d left! keeping buying" %salary 
        else:
            if salary < min(prices):
                print "\033[31;1mYou can't buy anything! Bye!\033[0m"
                print "you have bought:",shop_list
                break
            else: 
                print "\033[31;1msorry,money is not enough,try other one!\033[0m"

    else:
        print "\033[31;1m%s not in the list,try other one\033[0m" %f_choice

二、dictionary

-------------------------------contact_list.txt------------------------------
1    zhangsan    IT     18212356434
2    lisi        HR     17482382344
3    wangwu      QA     12384722344
4    sunliu      JN     14234234235
5    tom         IT     23423343523

-------------------------------contact_dic.py--------------------------------
#!/usr/bin/python
#coding=utf-8
import tab
contact_dic = {}

contact_file='contact_list.txt'
f = file(contact_file)
for line in f.readlines():
    name = line.split()[1]
    contact_dic[name] = line
#print contact_dic
#--------格式化输出-------------------------------------------------

for n,v in contact_dic.items():
    print n,'\t',v,

while True:
    input = raw_input("please input the staff name:").strip()
    if len(input) == 0:continue
    if contact_dic.has_key(input):
        print "\033[31;1m%s \033[0m" %contact_dic[input]
    else:
        print "sorry,no staff name found"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值