Search code examples
pythonfunctionwhile-loopprogram-entry-point

Functions And While Loop Complication In Python


def main():
    totalprofit = 0
    stockname = input("Enter the name of the stock or -999 to quit: ")
    while stockname != "-999":
       sharesbought, purchasingprice, sellingprice, brokercommission = load()
       amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss = calc(sharesbought, purchasingprice, sellingprice, brokercommission)
       output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofpaidcommission, profitorloss)
       stockname = input("Enter the name of the next stock (or -999 to quit): ")

       totalprofit += profitorloss
    print("\n Total profit is: ", format(totalprofit, '.2f'))

def load():
    sharesbought = int(input("Number of shares bought: "))
    purchasingprice = float(input("Purchasing price: "))
    sellingprice = float(input("Selling price: "))
    brokercommission = float(input("Broker commission: "))
    return sharesbought, purchasingprice, sellingprice, brokercommission

def calc(sharesbought, purchasingprice, sellingprice, brokercommission):
    amountpaid = sharesbought * purchasingprice
    amountofpaidcommission = amountpaid * (brokercommission/100)
    amountstocksoldfor = sharesbought * sellingprice
    amountofsoldcommission = amountstocksoldfor * (brokercommission/100)
    profitorloss = (amountpaid + amountofpaidcommission) - (amountstocksoldfor - amountofsoldcommission)
    return amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss

def output(stockname, amountpaid, amountofpaidcommission, amountstocksoldfor, amountofsoldcommission, profitorloss,):
    print("\n Stock name: ", stockname, sep = '')
    print("Amount paid for the stock: ", format(amountpaid, '.2f'))
    print("Commission paid to broker when the stock was bought: ", format(amountofpaidcommission, '.2f'))
    print("Amount the stock sold for: ", format(amountstocksoldfor, '.2f'))
    print("Commission paid to broker when the stock was sold: ", format(amountofsoldcommission, '.2f'))
    print("Profit or loss: ", format(profitorloss, '.2f'))

main ()

The objective of the first function is to allow a user to input the followings as many times as she or he wants until the user decides is done:

  1. Stock name
  2. Shares bought
  3. Selling price
  4. Broker commission

My main problem is then in the main function. I am skeptical whether I am using the while loop correctly or if it's correct at all. I tried to run the program but it won't output anything.

Also, shouldn't I add this at the end of the program with the values inputted to call all the functions above:

def main()
   load()
   calc()
   output()

Or is it fine within the while loop?


Solution

  • I think a while loop is perfectly appropriate for this use case, where you want to loop an indeterminate number of times, stopping when some condition is not met.

    There is one obvious problem, on this line:

    stockname +=1
    

    This doesn't make any sense. Since stockname is a string, you can't add one to it. Instead, you should be asking the user for the next stock name (or a "special" value to signal they're done). Try replacing that line with something like:

    stockname = input("Enter the name of the next stock (or -999 to quit): ")
    

    The rest of your code appears correct, if rather verbose. Unless you think it's likely you'll call some of your other functions in some other place in your code, it might be simpler and cleaner to include all the logic in one function. Functions are nice, but you should balance the benefits of isolating each part of your code in its own function against the effort of passing lots of values between them.