I'm having an issue with my program for some homework I'm working on. I have multiple while loops in my program and it seems that the ones after the first one are causing the first one to just re-print the data I've already had the user input.
repeat = 'y'
p = 'y'
b = 'y'
s = 'y'
while repeat != 'n':
while p == 'y':
stocksPurchased = float(input("Number of stocks purchased: "))
if stocksPurchased < 0:
print("Negative Values are not allowed. Please re-enter.")
else:
p = 'n'
while b == 'y':
pricePerStockBought = float(input("Amount per stock purchased in $: "))
if pricePerStockBought < 0:
print("Negative Values are not allowed. Please re-enter.")
else:
b = 'n'
while c == 'y':
commissionWhole = float(input("Commission Rate as a percent %: "))
if commissionWhole < 0:
print("Negative Values are not allowed. Please re-enter.")
else:
c = 'n'
while s == 'y':
pricePerStockSold = float(input("Amount per stock sold in $: "))
if pricePerStockSold < 0:
print("Negative Values are not allowed. Please re-enter.")
else:
s = 'n'
commissionRate = commissionWhole/100
grossPurchasePrice = stocksPurchased*pricePerStockBought
purchaseCommission = grossPurchasePrice*commissionRate
totalPurchasePrice = grossPurchasePrice+purchaseCommission
grossSalesPrice = stocksPurchased*pricePerStockSold
saleCommission = grossSalesPrice*commissionRate
netSalePrice = grossSalesPrice-saleCommission
totalCommissionPaid = purchaseCommission+saleCommission
profit = netSalePrice-totalPurchasePrice
profitPercentage = (profit/grossPurchasePrice)*100
print("Commission Fee paid after buying: $", format(purchaseCommission, ',.2f'))
print("Amount stock sold for: $", format(grossSalesPrice, ',.2f'))
print("Commission Fee paid after selling: $", format(saleCommission, ',.2f'))
print("Total Commission Paid: $", format(totalCommissionPaid, ',.2f'))
print("Total Profit made: $", format(profit, ',.2f'))
print("Profit Percentage: %", format(profitPercentage, ',.1f'))
if profitPercentage >= 8:
print("Congrats! You beat the index fund!")
elif 0 <= profitPercentage < 8:
print("Well, you still made money")
elif profitPercentage == 0:
print("Nothing gained, nothing lost")
else:
print("Perhaps the stock market isn't for you")
if totalCommissionPaid > profit:
print("Seems you should either pick different stocks, or find a cheaper broker")
repeat = input("Would you like to go again y/n?: ")
If I enter y here the program repeats but instead of re-prompting for the numbers it just re-prints the data from the previous run.
For example if I enter in the numbers: 1000, 10, 5, 15 respectively it will just reprint the same numbers from before.
p,b,sto'y'each time the outer loop repeats.