liugan 2018-04-18
概要
按理说,我们入门的第一个小程序都应该是Hello World。因为比较简单,我这也就不做过多的演示 了。
下面是我写的一个小程序。主要用于练习Python的基本语法,以及入门。
主要实现功能
代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @author: 烽火 @license: Apache Licence @file: shopping.py @time: 6/16/17 10:05 AM """ goods = [ ("IPhone", 5800), ("Watch", 2000), ("MacBook", 12000)] goods_cart = [] mybudget = input("请输入您的预算:") # 不考虑是小数的情况 while not mybudget.isdigit(): mybudget = input("输入有误,请重新输入您的预算:") mybudget = int(mybudget); while True: print("商品列表".center(50, "-")) print("编号".center(8, " "), "名称".ljust(30, " "), "价格".ljust(10, " ")) for i in enumerate(goods): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10, " ")) user_choose = input("请输入您的选择:") if user_choose.isdigit(): user_choose = int(user_choose) if user_choose >= 0 and user_choose < len(goods): if (mybudget - goods[user_choose][1]) >= 0: goods_cart.append(goods[user_choose]) mybudget -= goods[user_choose][1] print("预算还有%d" % mybudget) else: print("预算不够啦~") else: print("不存在该商品~") elif user_choose == 'q': break else: print("您的输入有误~") print("预算还剩%d了" %(mybudget)) print("购物车商品信息".center(50, "-")) for i in enumerate(goods_cart): print(str(i[0]).center(10, " "), str(i[1][0]).ljust(31, " "), str(i[1][1]).ljust(10, " "))
运行结果