0

小白

我正在尝试编写一个提供运行平衡的脚本。我搞砸了python的基本声明函数。

我也需要它:

  • 通过输入接受余额
  • 附加交易列表
  • 按照输入的顺序一一取出
  • 打印运行总计
  • 使用pyhtmltable以 html 表格的形式输出,以便复制和粘贴

代码:

# transaction posting on available balance

import PyHtmlTable 
import twodarr
import string,re
import copy
import sys

posting_trans = [] #creating a list of posting debits here

avail_bal = int(input('What is the balance available to pay transactions?')) #getting the starting balance

while True:  #building up the list of transactions
    ans = input('Please enter the debits in order of posting one at a time.  If there is no more, please enter 0:')
    if int(ans) == 0:
        break
    if ans > 0:    # to get out of loop
        posting_trans.append(ans)

num_trans = int(len(posting_trans))   #counting the number of transactions

print "<b> Beginning available balance of",avail_bal," </b> "  # start of the html table

tabledict = {'width':'400','border':2,'bgcolor':'white'}

t  = PyHtmlTable.PyHtmlTable( 2, 1 , tabledict )

t.setCellcontents(0,0,"Transactions")  #header cells
t.setCellcontents(1,0,"Available Balance")

while True:      #trying to create the rest of a dynamic table
    if countdown == 0:
        break

    for countdown in range(1,num_trans):
        t.add_row(1)

        def newer_bal():
            newer_bal(avail_bal - posting_trans[countdown])

            t.setCellcontents(0, 1, posting_trans[countdown])
            t.setCellcontents(1, 1, newer_bal)       

t.display()
4

1 回答 1

2

类似的东西?

# transaction posting on available balance
import PyHtmlTable 

posting_trans = [] #creating a list of posting debits here

#getting the starting balance
print 'What is the balance available to pay transactions? '
avail_bal = float(raw_input('Value: ')) 

while True:  #building up the list of transactions
    print 'Please enter the debits in order of posting one at a time.'
    print 'If there is no more, please enter 0:'
    ans = float(raw_input('Value: '))
    if ans == 0:
        break # to get out of loop
    posting_trans.append(ans)

# start of the html table
print "<b> Beginning available balance of %.2f</b>" % avail_bal

tabledict = {'width': '400', 'border': 2, 'bgcolor': 'white'}
t  = PyHtmlTable.PyHtmlTable(2, 1, tabledict)

t.setCellcontents(0, 0, "Transaction Value")  #header cells
t.setCellcontents(0, 1, "Available Balance")


for line, trans in enumerate(posting_trans):
    avail_bal -= trans
    t.setCellcontents(line + 1, 0, '%.2f' % trans)
    t.setCellcontents(line + 1, 1, '%.2f' % avail_bal)       

t.display()

提示:

  • 不要使用input(). 改为使用raw_input()。它已input()在 python 3.0 中重命名为。
  • 您不需要将值存储在列表中。您已经可以将它们存储在表中,这就是使用PyHtmlTable. 我出于教学目的离开了名单。
  • 阅读教程。阅读文档。写很多代码。
于 2009-01-23T13:40:12.237 回答