模块化
2016-01-31 15:31
302 查看
“格式化”字符串
“%s %e” % ("Value is", 16.0 ** 0.5) 显示一个字符串,后面跟着4.000000e+00。
"%7" % (11232/3) 显示计算结果,并用空格填充。
"%X" % 127 用16进制显示数字。
"%20s\n" % "Banana swirl" 把字符串填充到20位长,然后显示一个换行字符。 \n意味着换行。
"%s is $%4.2f" % (“Popsicle”, 1.754) 和字符串一样,显示一个2位十进制的浮点数。(16进制数会用来表示网页颜色之类的东西。)
“%s %f” % ("Value is",16.0 ** 0.5) 显示一个字符串,后面跟着4.000000。
“%07d” % (11232/3) 显示用0填充的值。 %后面的0意味着“用0填充”。
一个简单的pos程序:
运行结果:
进行模块化:
两个程序共用的程序(模块)
两个程序运行的结果:
价格下降了两次进行打折:
打9折:
打95折:
运行结果:
“%s %e” % ("Value is", 16.0 ** 0.5) 显示一个字符串,后面跟着4.000000e+00。
"%7" % (11232/3) 显示计算结果,并用空格填充。
"%X" % 127 用16进制显示数字。
"%20s\n" % "Banana swirl" 把字符串填充到20位长,然后显示一个换行字符。 \n意味着换行。
"%s is $%4.2f" % (“Popsicle”, 1.754) 和字符串一样,显示一个2位十进制的浮点数。(16进制数会用来表示网页颜色之类的东西。)
“%s %f” % ("Value is",16.0 ** 0.5) 显示一个字符串,后面跟着4.000000。
“%07d” % (11232/3) 显示用0填充的值。 %后面的0意味着“用0填充”。
一个简单的pos程序:
def save_transaction(price,credit_card,description): file=open("transactions.txt", "a") file.write("%16s%07d%s\n" % (credit_card, price * 100, description)) file.close() items=["DONUT","LATTE","FILTER","MUFFIN"] prices=[1.50,2.0,1.80,1.20] running=True while running: option=1 for choice in items: print(str(option)+"."+choice) option=option+1 print(str(option)+".Quit") choice=int(input("Choose an option:")) if choice==option: running=False else: credit_card=input("Credit card number:") save_transaction(prices[choice-1],credit_card,items[choice-1])
运行结果:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:2 Credit card number:63827462387648620000220 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:3 Credit card number:63827462387648320000180 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:5 >>>
进行模块化:
from transactions import * items=["WORKOUT","WEIGHTS","BIKES"] prices=[35.0,10.0,8.0] running=True while running: option=1 for choice in items: print(str(option)+"."+choice) option=option+1 print(str(option)+".Quit") choice=int(input("Choose an option:")) if choice==option: running=False else: credit_card=input("Credit card number:") save_transaction(prices[choice-1],credit_card,items[choice-1])
from transactions import * items=["DONUT","LATTE","FILTER","MUFFIN"] prices=[1.50,2.0,1.80,1.20] running=True while running: option=1 for choice in items: print(str(option)+"."+choice) option=option+1 print(str(option)+".Quit") choice=int(input("Choose an option:")) if choice==option: running=False else: credit_card=input("Credit card number:") save_transaction(prices[choice-1],credit_card,items[choice-1])
两个程序共用的程序(模块)
def save_transaction(price,credit_card,description): file=open("transactions.txt", "a") file.write("%07d%16s%16s\n" % ( price * 100, credit_card, description)) file.close()
两个程序运行的结果:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:1 Credit card number:6432425412474321 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:5 >>> ================================ RESTART ================================ >>> 1.WORKOUT 2.WEIGHTS 3.BIKES 4.Quit Choose an option:1 Credit card number:7649463856424326 1.WORKOUT 2.WEIGHTS 3.BIKES 4.Quit Choose an option:4 >>>
价格下降了两次进行打折:
from transactions import * import promotion import starbuzz1 items=["DONUT","LATTE","FILTER","MUFFIN"] prices=[1.50,2.0,1.80,1.20] running=True while running: option=1 for choice in items: print(str(option)+"."+choice) option=option+1 print(str(option)+".Quit") choice=int(input("Choose an option:")) if choice==option: running=False else: credit_card=input("Credit card number:") price=promotion.discount(prices[choice-1]) if input("Starbuzz card")=="Y": price=starbuzz1.discount(price) save_transaction(price,credit_card,items[choice-1])
打9折:
def discount(price): return 0.9*price
打95折:
def discount(price): return 0.95*price
运行结果:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:4 Credit card number:5413765853576543 Starbuzz cardN 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:2 Credit card number:5413765853576543 Starbuzz cardY 1.DONUT 2.LATTE 3.FILTER 4.MUFFIN 5.Quit Choose an option:
相关文章推荐
- Treap平衡树
- 阿里巴巴利润暴涨108%
- RPI的入门教程(树莓派)
- CentOs6.7环境下rpm包安装mysql5.7
- 每天吃多少才不会胖?食物和卡路里对照表
- POJ 2253 —— Frogger
- 年末总结——关于读书学习
- MacDown 编辑器 使用
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- Android NDK开发基础——配置及简单案例
- 《Learn You Some Erlang for Great Good!》的学习笔记(四)
- BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue
- 数据结构和算法学习
- OpenPyXL学习
- HUD1754 (完全二叉树)
- hdu 5615 Jam's math problem(判断是否能合并多项式)
- POJ2502(最短路径dijkstra)
- 图的邻接表
- 一个对象怎么调用另一个对象, 打jar包,到底调用哪一个方法, 匿名对象, 空对象
- bzoj3674 可持久化并查集加强版