Frosty Elsa Frosty Elsa - 2 months ago 17
Python Question

While loop not printing inputs, quitting doesn't break out of while loop

#Initialization
count=0
name=0

#Input
while name!='-999':
count=count+1
name=input("Enter stock name OR -999 to Quit:")
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)

#Output
print("Stock 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'))

===============================OUTPUT===================================================
Enter stock name OR -999 to Quit:Google
Enter number of shares:10000
Enter purchase price:39.99
Enter selling price:35.99
Enter commission:0.04
Enter stock name OR -999 to Quit:Amazon
Enter number of shares:10000
Enter purchase price:15.99
Enter selling price:33.99
Enter commission:0.04
Enter stock name OR -999 to Quit:-999
Enter number of shares:10000
Enter purchase price:14.99
Enter selling price:49.99
Enter commission:0.04
Stock Name: -999
Amount paid for the stock: $ 149,900.00
Commission paid on the purchase: $ 5,996.00
Amount the stock sold for: $ 499,900.00
Commission paid on the sale: $ 19,996.00
Profit (or loss if negative): $ 324,008.00


As you can see I'm inputting stocks for Google and Amazon, but they're not being printed when I run the "-999" to break out of the while loop. Instead, it thinks that "-999" is a Stock name and quits when you enter fake numbers for it and prints those instead. I can't figure out what I'm doing wrong.

Answer

You should break out of the loop as soon as you get -999, instead of continuing the current iteration right until the end.

Secondly you could collect the items in a string and only output that after the loop:

#Initialization
count=0
name=0
result = ''
#Input
while True:
    count=count+1
    test=input("Enter stock name OR -999 to Quit:")
    if test=='-999': break
    name=test
    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)

    #Output
    result += """
Stock Name: {}
Amount paid for the stock:       ${:06.2f}
Commission paid on the purchase: ${:06.2f}
Amount the stock sold for:       ${:06.2f}
Commission paid on the sale:     ${:06.2f}
Profit (or loss if negative):    ${:06.2f}
""".format(name, amount_paid, commission_paid_purchase, amount_sold, commission_paid_sale, profit_loss)

print (result);

See it run on repl.it

Comments