python编程超市购物系统_通过python实现超市购物系统(通过列表简单实现版)

通过导入外部文件来导入数据:

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 os

import 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

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‘

原文:http://3971212.blog.51cto.com/3961212/1630305

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值