while循环到另一个文件

时间:2016-04-09 03:41:42

标签: function python-3.x while-loop

感谢您查看此信息。所以我遇到了一些问题。

  1. 我似乎无法将所需的输出转换为另一个文件。我试图在某种意义上模拟收据,以显示项目,数量,项目成本等的描述。它似乎没有正常工作,通常在那里放一个负面。我知道这与输入负数有关,但我对此感到困惑。

  2. 我似乎无法正确格式化。我想将所有内容都舍入到两个小数位,我知道它是{:.2f}但是,因为我试图在列中排列这些,所以我似乎无法弄清楚这一点。

  3. 我的代码:

    def cashRegister():
        subTotal = 0
        total = 0
        salesTax = .085
        registerTableHeader = "{0:<15}{1:>35}{2:>10} {3:>10}{4:>10}\n"\
                               .format('Description', 'Qty', 'Unit Cost',\
                                 'Item Cost', 'Subtotal')
        cashRegisterFile.write(str(registerTableHeader))
    
    
        quantityOfItem = int(input("Enter quantity of item: "))
        while(quantityOfItem > 0):
            itemDescription = str(input("Enter Item's description: ")[:30])
            itemCost = float(input("Enter item's cost: "))        
            subTotal = quantityOfItem * itemCost 
            total += subTotal
            quantityOfItem = int(input("Enter quantity of item: "))
    
        registerTable = "{0:<15}{1:>35}{2:>10} {3:>10} {4:>10}\n\n"\
                .format(itemDescription, quantityOfItem, itemCost, itemCost * \
                    quantityOfItem, subTotal)      
        cashRegisterFile.write(str(registerTable))
    

    期望的输出:

    Description                     Qty Unit Cost Item Cost  Subtotal
    First Item's Description          1     24.99     24.99     24.99
    This is the second item's desc    5      2.00     10.00     34.99
    This is the third item's descr   10     11.00    110.00    144.99
    
    Sales Tax                                                   13.05
    Total                                                      158.04
    

    感谢您查看我的帖子!

1 个答案:

答案 0 :(得分:1)

您需要缩进对项目的书写,以便对SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); String activityTag = editor.getString(getString(R.string.last_viewed_activity_tag), null); // If the user is not launching the app for the first time, launch the last opened Activity; otherwise, launch the default initial Activity. Class initialActivityClass; switch (activityTag) { case "CoolActivity": initialActivityClass = CoolActivity.class; break; case "WowActivity": initialActivityClass = WowActivity.class; break; default: initialActivityClass = MainActivity.class; break; } Intent intent = new Intent(this, initialActivityClass); startActivity(intent); finish(); 循环中的每个项目执行。你只在循环后写了这个项目。因此,条件while中的负数已写入文件。

我将循环的顶部更改为:

while(quantityOfItem > 0):

将您的数字格式化为 while True: quantityOfItem = int(input("Enter quantity of item: ")) if quantityOfItem <= 0: break 两位小数:

{4:>10.2f}

def cashRegister(cashRegisterFile): subTotal = 0 total = 0 salesTax = .085 registerTableHeader = ("{0:<30} {1:>10}{2:>10} {3:>10} {4:>10}\n".format( 'Description', 'Qty', 'Unit Cost','Item Cost', 'Subtotal')) cashRegisterFile.write(registerTableHeader) while True: quantityOfItem = int(input("Enter quantity of item: ")) if quantityOfItem <= 0: break itemDescription = input("Enter Item's description: ")[:30] itemCost = float(input("Enter item's cost: ")) subTotal = quantityOfItem * itemCost total += subTotal registerTable = ("{0:<30} {1:>10}{2:>10.2f} {3:>10.2f} {4:>10.2f}\n".format( itemDescription, quantityOfItem, itemCost, itemCost * quantityOfItem, subTotal)) cashRegisterFile.write(registerTable) with open('cash.txt', 'w') as cashRegisterFile: cashRegister(cashRegisterFile) 的内容:

cash.txt