[Tutor] input loop

Alan Gauld alan.gauld at btinternet.com
Mon Sep 2 01:41:50 CEST 2013


On 01/09/13 22:53, Nick Wilson wrote:

> Enter command: print
> ----------------------------------
> Code     Price   Quant       Value
> ----------------------------------

The bit above needs to be printed once, at the start


> TPW       2.00       5       10.00

This bit needs to be repeated for each share type


> ----------------------------------
> Total cost:                  10.00
> ==================================

And this bit needs to be printed once at the end
after doing the calculations.

> **(This is all even when displayed in Wing101)

That will depend on fonts used etc. If you really need it aligned neatly 
I recommend using html and displaying it in a browser,
but its a lot more work that way!


> 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: '

Were you ever a COBOL programmer by any chance?

> def main():
>      """This is the main function for the program, ie, this function is
> called
>      first when the program is run"""

There is nothing in Python that will run this first. You need to call it 
explicitly. This is often done inside a name test clause:

if __name__ == "__main__":
     main()



>      command = input(PROMPT_FOR_COMMAND).lower()
>      if command == 'add':
>          return portfolio.append(process_add_code(portfolio))

Are you sure you want to return here?
That will terminate the main function without saving portfolio.
You may want to create a menu system where you repeatedly ask
for input until quit is selected.


>      if command == "print" and len(portfolio) <= 1:
>          print_portfolio_table(portfolio)
>          return main()
>      if command == "quit":
>          print(MESSAGE_GOODBYE)

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list