zoukankan      html  css  js  c++  java
  • 模块化

    “格式化”字符串

    “%s %e”  %   ("Value is", 16.0 ** 0.5)       显示一个字符串,后面跟着4.000000e+00。

    "%7"   %   (11232/3)       显示计算结果,并用空格填充。

    "%X"    %   127       用16进制显示数字。

    "%20s "    %    "Banana swirl"       把字符串填充到20位长,然后显示一个换行字符。   意味着换行。

    "%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
    " % (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
    " % ( 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:
    

      

  • 相关阅读:
    Android AsyncTask
    Eclipse 快捷键
    Android JSON数据的读取和创建
    Android 原生listview item伸展收缩效果 (续)
    Android 原生listview item伸展收缩效果
    Android listview 禁止滑动
    Android R.layout. 找不到已存在的布局文件
    Android ScrollView
    Android android:clickable 问题
    Codeforces 388C Fox and Card Game (贪心博弈)
  • 原文地址:https://www.cnblogs.com/lizanqirxx/p/5163161.html
Copyright © 2011-2022 走看看