TheEWL
TheEWL

Reputation: 19

While Loop in function ( Python )

So I basically created my functions ( def main(), load(), calc(), and print(). But I do not know how I can allow the user to input the information as many times as he/she wants until they want to stop. Like I they input 5 times, it would also output 5 times. I have tried putting the while loop in the def main() function and the load function but it won't stop when I want it to. Can someone help? Thanks!

def load():

    stock_name=input("Enter Stock Name:")
    num_share=int(input("Enter Number of shares:"))
    purchase=float(input("Enter Purchase Price:"))
    selling_price=float(input("Enter selling price:"))
    commission=float(input("Enter Commission:"))

    return stock_name,num_share,purchase,selling_price,commission

def calc(num_share, purchase, selling_price, commission):

    paid_stock = num_share * purchase
    commission_purchase = paid_stock * commission
    stock_sold = num_share * selling_price
    commission_sale = stock_sold * commission
    profit = (stock_sold - commission_sale) - ( paid_stock + commission_purchase)
    return paid_stock, commission_purchase, stock_sold, commission_sale, profit

def Print(stock_name,paid_stock, commission_purchase, stock_sold, commission_sale, profit):

    print("Stock Name:",stock_name)
    print("Amount paid for the stock:\t$",format(paid_stock,'10,.2f'))
    print("Commission paid on the purchase:$", format(commission_purchase,'10,.2f'))
    print("Amount the stock sold for:\t$", format(stock_sold,'10,.2f'))
    print("Commission paid on the sale:\t$", format(commission_sale,'10,.2f'))
    print("Profit(or loss if negative):\t$", format(profit,'10,.2f'))  

def main():

    stock_name,num_share,purchase,selling_price,commission = load()
    paid_stock,commission_purchase,stock_sold,commission_sale,profit = calc(num_share, purchase, selling_price, commission)
    Print(stock_name, paid_stock,commission_purchase, stock_sold, commission_sale, profit)

main()

Upvotes: 1

Views: 928

Answers (2)

Adele
Adele

Reputation: 39

an even simpler way would be two do the following....

while True:
    <do body>
    answer = input("press enter to quit ")
    if not answer: break

alternatively initialize a variable and avoid the inner if statement

sentinel = True
while sentinel:
     <do body>
     sentinel = input("Press enter to quit")

if enter is pressed sentinel is set to the empty str, which will evaluate to False ending the while loop.

Upvotes: 1

Vsevolod Timchenko
Vsevolod Timchenko

Reputation: 756

You have to give the user some kind of way to declare their wish to stop the input. A very simple way for your code would be to include the whole body of the main() function in a while loop:

response = "y"
while response == "y":
    stock_name,num_share,purchase,selling_price,commission = load()
    paid_stock,commission_purchase,stock_sold,commission_sale,profit = calc(num_share, purchase, selling_price, commission)
    Print(stock_name, paid_stock,commission_purchase, stock_sold, commission_sale, profit)
    response = input("Continue input? (y/n):")

Upvotes: 2

Related Questions