0

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.

Example of the issue I am having

3
  • 1
    Looks like you need to reset p, b, s to 'y' each time the outer loop repeats. Commented Apr 2, 2018 at 3:47
  • This worked. Thank you! Commented Apr 2, 2018 at 3:50
  • 2
    No problems, please try to reduce your sample code as much as possible next time (see mcve). This can help with debugging and the mistake would have been a lot clearer, it's likely you would have even picked it up before having to post the question. Commented Apr 2, 2018 at 3:53

6 Answers 6

2

Set the values of p, b, c and s to 'y'.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

You need to write these lines:

p = 'y'
b = 'y'
s = 'y'

inside the main while loop. Then it will solve your problem.

Comments

0

You need to reset the conditionals inside the first while loop.

That is put

p = 'y'
b = 'y'
s = 'y'

After your last line

Comments

0

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.

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.