[Tutor] input loop

Nick Wilson nickwilson88 at hotmail.com
Sun Sep 1 23:53:43 CEST 2013


Hi,
I am trying to create a portfolio of shares, each share is entered manually while checking it against a list of actual share codes.Its working mostly as intended at the moment, except when I print the table out at the end, I want all entered details printed as such
Enter command: addEnter share code to add: tpwTPW = TrustPowerEnter share price: 2Enter share quantity: 5Enter command: print----------------------------------Code     Price   Quant       Value----------------------------------TPW       2.00       5       10.00----------------------------------Total cost:                  10.00==================================
**(This is all even when displayed in Wing101)
So thats all fine, but I'm wanting muiltiple more rows printed underneath with other shares and their prices etc. Getting stuck on that, I know i'll need a for loop to iterate over the input but still wanting to get some help.
here's the rest of the code so far.




PROMPT_FOR_COMMAND = 'Enter command: 'PROMPT_FOR_CODE = 'Enter share code to add: 'PROMPT_FOR_PRICE = 'Enter share price: 'PROMPT_FOR_QUANTITY = 'Enter share quantity: '
MESSAGE_UNKNOWN_COMMAND ='Unknown command - please try again.'MESSAGE_UNKNOWN_CODE = 'Unknown code - CANCELLING code entry.'MESSAGE_PORTFOLIO_EMPTY = 'No shares in portfolio.'MESSAGE_GOODBYE = 'Thankyou and goodbye.'
TITLE_TEMPLATE = '{:<6}{:>8}{:>8}{:>12}'LINE_ITEM_TEMPLATE = '{:<6}{:>8.2f}{:>8}{:>12.2f}'LINE_TOTAL_TEMPLATE = '{:<20}{:>14.2f}'SPACER_LINE = 34 * '-'DBL_SPACE_LINE = 34 * "="
def print_portfolio_table(portfolio):    """ Prints out a portfolio table as per specification.    You can use the test_print_portfolio_table to test this function..."""    print(SPACER_LINE)    print(TITLE_TEMPLATE.format("Code", "Price", "Quant", "Value"))    print(SPACER_LINE)    print(LINE_ITEM_TEMPLATE.format(code, price, quant, (price * quant)))    print(SPACER_LINE)    print(LINE_TOTAL_TEMPLATE.format("Total cost:", (price * quant)))    print(DBL_SPACE_LINE)
def process_add_code(portfolio):    """Receives a portfolio list.    Asks for a share code.    If it's in the exchange then prints code = fullname    then gets price and quantity from the user    then it appends the (code, price, quantity) to the portfolio"""    global code    code = input(PROMPT_FOR_CODE).upper()    if shares.in_exchange(code) == True:        print(code + " " + "=" + " " + shares.get_name(code))        global price        price = float(input(PROMPT_FOR_PRICE))        global quant        quant = int(input(PROMPT_FOR_QUANTITY))        portfolio = portfolio.append([code, price, quant])        main()    else:        print(MESSAGE_UNKNOWN_CODE)        main()
def main():    """This is the main function for the program, ie, this function is called     first when the program is run"""    # The portfolio list will contain tuples as (code, price, quantity)    # Initially it is empty    portfolio = []    command = input(PROMPT_FOR_COMMAND).lower()    if command == 'add':        return portfolio.append(process_add_code(portfolio))    if command == "print" and len(portfolio) <= 1:        print_portfolio_table(portfolio)        return main()    if command == "quit":        print(MESSAGE_GOODBYE)    
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130902/6a2a0346/attachment-0001.html>


More information about the Tutor mailing list