0

I'm studying programming with Python but I can't solve the problem now.

In the 11th and 13th rows, if withdrawal is denied, I want to make it to repeat the 10th row input("Enter amount of withdrawal")...but using continue goes to selection=input("Make a selection from the option menu:"). What should I do?

balance=1000
print("Options:\n1. Make a Deposit\n 2. Make a Withdrawal\n3. Obtain Balance\n4. Quit")
while True:
selection=input("Make a selection from the option menu:")
if selection=='1':
    deposit=float(input("Enter amount of deposit:"))
    balance+=deposit
    print("Deposit Processed.")
if selection=='2':
    withdrawal=float(input("Enter amount of withdrawal:"))
    if withdrawal>balance:
        print("Denied. Maximum withdrawal is $","{0:,.2f}".format(balance))
        continue                  # ***In this process, if withdrawal is denied, I wanna the 10th row..but using continue goes to selection=input("Make a selection from the option menu:") . What should I do?***
    if withdrawal<=balance:
        balance-=withdrawal
        print("Withdrawal Processed.")
if  selection=='3':
    print('$','{0:,.2f}'.format(balance))
if selection=='4':
    break
martineau
  • 119,623
  • 25
  • 170
  • 301
Chloe
  • 17
  • 2
  • 3
    The code you've posted is not valid right now. Python is very sensitive to indentation, so please be sure to correctly indent the code you share with us. – Silvio Mayolo Apr 03 '21 at 16:45
  • Please [don’t post images of code or error messages.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Apr 03 '21 at 16:54
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Apr 03 '21 at 17:54

1 Answers1

0

Assuming that everything from the 4th line onwards is in the while loop, the only thing you need to add is another for loop within the if selection=='2' block:

if selection == '2':
    while True:
        withdrawal = float(input("Enter amount of withdrawal:"))

        if withdrawal > balance:
            print("Denied. Maximum withdrawal is $", "{0:,.2f}".format(balance))
        else:
            balance -= withdrawal
            print("Withdrawal Processed.")
            break

Sidenote: instead of using consecutive ifs, you might want to consider using elif and else statements to make your code more readable:

balance = 1000
print("Options:\n1. Make a Deposit\n2. Make a Withdrawal\n3. Obtain Balance\n4. Quit")
while True:
    selection = input("Make a selection from the option menu:")
    if selection == '1':
        deposit = float(input("Enter amount of deposit:"))
        balance += deposit
        print("Deposit Processed.")
    elif selection == '2':
        while True:
            withdrawal = float(input("Enter amount of withdrawal:"))

            if withdrawal > balance:
                print("Denied. Maximum withdrawal is $", "{0:,.2f}".format(balance))
            else:
                balance -= withdrawal
                print("Withdrawal Processed.")
                break
    elif selection == '3':
        print('$', '{0:,.2f}'.format(balance))
    elif selection == '4':
        break
ttyip
  • 1,231
  • 1
  • 13
  • 21
  • @Chloe you're welcome! If this answered your question, please consider marking it as accepted! Thanks! – ttyip Apr 04 '21 at 17:50