python成品代码_Python小作业二(购买物品)(示例代码)

该博客展示了一个使用Python编写的模拟购物程序,用户输入工资后可以选择多种物品,程序会根据用户选择的商品计算是否可以支付,并打印购买的物品和剩余金额。用户可以选择继续购物或结束。
摘要由CSDN通过智能技术生成

要求:

1、输入工资后,提供多种物品供选择。

2、有物品菜单及价格。

3、选择商品计算用户是否可以支付。

4、打印用户所购买的物品。

5、输出用户剩余的钱,问用户是否购物,结束。

逻辑图:

代码如下:#!/usr/bin/env python

from tabulate import tabulate

import time

import sys

goods = [["1","car",20000],["2","iphone",5000],["3","computer",3000],["4","watch",1000],["5","T-short",500],["6","shoes",200],["7","hamburg",50]]

header = ["list","goods","price"]

list = tabulate(goods,header,tablefmt="grid")

print ""

print "\033[32;1mWelcome to materials  Supermarket,enjoy your self\033[0m"

print ""

while True:

salary = raw_input("\033[33;1mPlease input your salary:\033[0m").strip()

if  not salary.isdigit():continue

#if not salary:continue

break

count = 0

form=[]

while True:

if int(salary) 

print "\033[31;1mYour salary not enough allow you consume,Say Good bay\033[0m"

if form == []:

print "\033[33;1myou get nothing\033[0m"

sys.exit()

else:

print "\033[33;1mYour buy follows:\033[0m"

print list3

print "\033[32;1mYou remain %s\033[0m" %salary

sys.exit()

print "\033[32;1mFollow is Our Supermarket support goods,please input quence number to select articles\033[0m"

print list

print "\033[34;1mYour Left is %s" %salary

list2 = raw_input("\033[32;1mPlease input your choice list-id:").strip()

if not list2.isdigit() or int(list2)  7:

print "\033[33;1myour input is not legal,please input again(1~7)\033[0m"

continue

price = goods[int(list2)-1][2]

article = goods[int(list2)-1][1]

if salary >= price:

salary = int(salary) - int(price)

count += 1

time2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

price = str(price)

count = str(count)

element = [count,time2,article,price]

count = int(count)

form.append(element)

header2 = ["list","Time","Article","Price"]

list3 = tabulate(form,header2,tablefmt="grid")

print "\033[33;1mYou got This:%s" %article

print list3

while True:

A = raw_input("\033[32;1mWould you want to buy another articles(Y|N):\033[0m")

if A == "Y" or A == "y":

break

elif A == "N" or A == "n":

print "\033[33;1mSay good bey,thankYou,your buy article is follow list:\033[0m"

print list3

print "\033[32;1mYou remain %s\033[0m" %salary

sys.exit()

else:

print "You input is not incorrect"

continue

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值