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
, c
和s
的值设置为'y'
。
The fix to this problem is easy: reset the values of p
, b
, s
, and c
at the beginning of the first while
loop:
repeat = 'y'
while repeat != 'n':
p = 'y'
b = 'y'
s = 'y'
c = 'y'
#Rest of code here
You need to initialize your variables within the while loop in order to fix this problem. This way, whenever you are done with iteration of the loop your variables will be restarted, so the condition for the next loops is satisfied. So your code should be:
while repeat != 'n':
repeat = 'y'
p = 'y'
b = 'y'
s = 'y'
c ='y'
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'
On top of that you should fix your indentation since it seems a little bit off.
You need to write these lines:
p = 'y'
b = 'y'
s = 'y'
inside the main while
loop. Then it will solve your problem.
You need to reset the conditionals inside the first while loop.
That is put
p = 'y'
b = 'y'
s = 'y'
After your last line
The problem is with your variables. After the first loop your variables p, b, c and s are assigned with 'n'. So the second loop take them as n no y You can try something like,
repeat = p = b = c = s = input("Would you like to go again y/n?: ").
There can be better methods. But the problem was with your variable values after the first loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.