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>It doesn't make sense for your few lines of code, as it blows up things unnecessarily, but here you go:</p>

<pre><code>def load():
    shares=int(input("Enter number of shares: "))
    pp=float(input("Enter purchase price: "))
    sp=float(input("Enter selling price: "))
    commission=float(input("Enter commission: "))
    return shares, pp, sp, commission

def calc(totalpr):
    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+=profit_loss
    return commission_paid_purchase, amount_paid, amount_sold, commission_paid_sale, profit_loss, totalpr

def 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'))


if __name__ == "__main__":
    #Initialization
    name=0
    count=0
    totalpr=0.0
    name=input("Enter stock name OR -999 to Quit: ")
    while name!='-999':
        count+=1
        #Load
        shares, pp, sp, commission = load()
        #Calculations
        commission_paid_purchase, amount_paid, amount_sold, commission_paid_sale, profit_loss, totalpr = calc(totalpr)
        #Output
        output()
        name=input("\nEnter stock name OR -999 to Quit: ")
print("Total Profit is $", format(totalpr, '10,.2f'))
</code></pre>

<p>Note that variables that are defined within the scope of the while loop are automatically visible by functions that a called from within that loop.</p>

python function python-3.x while-loop