Converting the contents of a while loop into a function
<pre><code>#Initialization
name=0
count=0
totalpr=0.0
#Load
name=input("Enter stock name OR -999 to Quit: ")
while name!='-999':
    count=count+1
    shares=int(input("Enter number of shares: "))
    pp=float(input("Enter purchase price: "))
    sp=float(input("Enter selling price: "))
    commission=float(input("Enter commission: "))
    #Calculations
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss
    #Output
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    name=input("\nEnter stock name OR -999 to Quit: ")
print("Total Profit is $", format(totalpr, '10,.2f'))

def main()

load() #to input the values
calc()
print()
</code></pre>

<p>As you can see, I don't know how to convert the contents of paragraph "#Calculations" into a function so that it can be called at the bottom. The same goes for "#Load" (I want all the inputs in there somehow, even though there is a while loop in between it). And also for the "#Output" otherwise known as "print()" I would like it to be converted to a function as well. </p>

<p>I've got the bottom part down right, but I don't know how to edit my code so that it fits neatly into the form of a function. The code itself is fine, I just need to find a way to convert to functions and then call it. I've got the calling part down, so I'm working backwards in a sense. </p>

<p>This is one way to convert inside of while loop to a function and then use the main function to call the function as many times as you like.</p>

<p>You can break this code down further to a print function. Just use return statement for that. </p>

<pre><code>def calculate(shares,pp,sp,commission,name):
    totalpr = 0
    amount_paid=int(shares)*float(pp)
    commission_paid_purchase=float(amount_paid*commission)
    amount_sold=int(shares)*float(sp)
    commission_paid_sale=float(amount_sold*commission)
    profit_loss=float(amount_sold - commission_paid_sale) -float(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))
    print("Total Profit is $", format(totalpr, '10,.2f'))
def main():
    name = input("Enter stock:")
    while name != '-999':
        calculate(5,1.22,11.33,11.44,name)
        name = input("Enter stock:")
main()
</code></pre>

python function python-3.x while-loop