通过导入外部文件来导入数据:
shop.txt
car 200000
iphone 4999bicycle 500cafe 35##########################################################
python源代码:
#!/usr/bin/env python# encoding=utf-8# author:sihaogongyuan# date:2015-4-9# filename:shop.py# desc:shop####################################import osimport sys####################################os.system('date')product_list=[]price_list=[]shop_list=[]####################################f=file('shop.txt','r')for line in f.readlines(): new_line=line.split() product_list.append(new_line[0]) price_list.append(int(new_line[1]))####################################salary=int(raw_input('\033[32mplease input your salary:\033[0m'))####################################while True: #pass print 'product list:' for p in product_list: p_index=product_list.index(p) p_price=price_list[p_index] print p,p_price choice=raw_input('please input your choice:') f_choice=choice.strip() while f_choice.strip=='': choice=raw_input('please input your choice:') f_choice=choice.strip() if f_choice in product_list: f_choice_index=product_list.index(f_choice) f_choice_price=price_list[f_choice_index] if salary>=f_choice_price: yes_or_no=raw_input('que ren goumai %s product?yes or no:'%(f_choice)) if yes_or_no=='yes': print '\033[32m%s have add shop_list\033[0m'%(f_choice) shop_list.append(f_choice) salary=salary-f_choice_price print '\033[32m余额是:\033[0m',salary else: print '\033[32mcancle %s into gouwuche...\033[0m'%(f_choice) print '\033[32m余额是:\033[0m',salary else: if salary