Вычесть из добавленного списка входных данных с выводом текущего баланса - PullRequest
0 голосов
/ 23 января 2009

Noob

Я пытаюсь написать сценарий, который дает текущий баланс. Я напортачил с простейшими объявленными функциями 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()

1 Ответ

2 голосов
/ 23 января 2009

Что-то в этом роде?

# 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. Я оставил список в дидактических целях.
  • Прочитайте учебник. Прочитайте документацию. Напишите много кода.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...