標籤:原始碼 file python import
通過匯入外部檔案來匯入資料:
shop.txt
car 200000
iphone 4999
bicycle 500
cafe 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<min(price_list): print ‘\033[31mnot buy anything!!!!\033[0m‘ print ‘\033[32mgouwuche you:\033[0m‘,shop_list print ‘\033[32m餘額是:\033[0m‘,salary sys.exit() else: print ‘\033[31m not afford %s,please try other\033[0m‘%(f_choice) print ‘\033[32m餘額是:\033[0m‘,salary else: print ‘\033[31m what you want is not founed,please try other\033[0m‘
通過python實現超市購物系統(通過列表簡單實現版)