1

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()
1
  • 2
    Although you did capitalize the first letter differently in your function Print() (caps in func names are against PEP 8) it's still a very poor choice to mirror built-ins like that. I strongly recommend changing the name. Commented Apr 4, 2016 at 1:36

2 Answers 2

2

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):")
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.