-1

The program is to accept a choice from the user.

Choice 1 to check the deposit

Choice 2 to make a deposit - when I entered choice two it should accept a deposit that is greater than 0 but less than 100,000, if this condition is not met then the program should prompt the user to enter the deposit until the condition is met.

If the condition is met, then the program should add the deposit entered to 30,000 and print the result.

My problem is even if the deposit is greater than 100,000 or less than 0, it is still printing the result and it should only prompt the user to enter the deposit until it is greater than 0 or less than 100,000.

What could be the error or problem in my code?

balance = 30000

choice = int(input("Enter 1 to Check Balance\nEnter 2 to deposit\nEnter 3 to withdrawl\n:"))
if choice == 1:
    print("Your balance is $",balance)
if choice == 2:
    while True:
        deposit= float(input("Enter the deposit you would like to make:"))
        if deposit < 0 or deposit > 100000:
            print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
    
       
        if(deposit < 0 and deposit > 100000):
            continue;
        newbalance = deposit + balance
        print("Your balance is $", newbalance)
         
Giuseppe La Gualano
  • 1,491
  • 1
  • 4
  • 24
  • This condition is always `False`: `deposit < 0 and deposit > 100000`. The opposite of your earlier statement is `not(deposit < 0 or deposit > 100000)`, which can be written `deposit >= 0 and deposit <= 100000` - you could also use `else` – mjsqu Oct 25 '22 at 05:20
  • Can you think of a value for *deposit* that fits the condition *deposit < 0 and deposit > 100000* ? Did you also notice that the value of *balance* never changes? – DarkKnight Oct 25 '22 at 06:09
  • Does this answer your question? [Why does non-equality check of one variable against many values always return true?](https://stackoverflow.com/questions/26337003/why-does-non-equality-check-of-one-variable-against-many-values-always-return-tr) – Sören Oct 26 '22 at 06:01

5 Answers5

0

The problem here is in the 2nd if condition of while loop. You shall use or logical operator instead of and as if(deposit < 0 and deposit > 100000): means that simultaneously deposit < 0 and greater than 100000 which is false always. Secondly don't use ; after continue

The code should be as follows: if (deposit < 0 or deposit > 100000): Hope this helps you!

0

Hey please see below for the code I think you want. From what I understand in terms of what you want, your problem was that your script was still going to print the deposit irrespective of invalid values. In your code, after the first if statement you are telling python to just continue, even if the first if statement evaluates to true.

balance = 30000

choice = int(input("Enter 1 to Check Balance\nEnter 2 to deposit\nEnter 3 to withdrawl\n:"))
if choice == 1:
    print("Your balance is $",balance)
if choice == 2:
    while True:
        deposit= float(input("Enter the deposit you would like to make:"))
        if deposit < 0 or deposit > 100000:
            print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
            continue
    
       
        #if(deposit < 0 and deposit > 100000):
            #continue;

        newbalance = deposit + balance
        print("Your balance is $", newbalance)
        break
0

Fixing conditions in original code:

balance = 30000

choice = int(input("Enter 1 to Check Balance\nEnter 2 to deposit\nEnter 3 to withdrawl\n:"))
if choice == 1:
    print("Your balance is $",balance)
elif choice == 2:
    while True:
        deposit= float(input("Enter the deposit you would like to make:"))
        if deposit < 0 or deposit > 100000:
            print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
            continue
    

        newbalance = deposit + balance
        print("Your balance is $", newbalance)

But the above code will keep on adding, better code can be:

while True:
    choice = int(input("Enter 1 to Check Balance\nEnter 2 to deposit\nEnter 3 to withdrawl\n:"))
    if choice == 1:
        print("Your balance is $",balance)
    elif choice == 2:
        deposit= float(input("Enter the deposit you would like to make:"))
        if deposit < 0 or deposit > 100000:
            print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
            continue
        newbalance = deposit + balance
        print("Your balance is $", newbalance)
HARSH MITTAL
  • 750
  • 4
  • 17
0

Firstly, You have the same condition in the two if statments on line 17 and 21. It can be replaced with one single IF statment.

So you're checking if the deposit is less than 0 or greater than 100000. If it fits that condition you want display an error message and re-prompt the user for the deposit withnin the stipulated range. When the user enters a number withnin the stipulated range the program should 'break' out of the while loop and print the balance.

If it fits the condition, display the error message and the use the 'continue' statment to restart the loop and prompt the user continously for a deposit until the user enters a number which fits the condition. if the user enters a deposit greater than 0 or less than 100000. You would want to break out of the loop using the 'break' statment and display the new deposit.

if(deposit < 0 or deposit > 100000):          
    print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
            continue
else:
    break

Also I think you intended to place the line 25 and 26 where you are calculating th new deposit and displaying the deposit outside the while loop. Lines 25 and 26 should only be exceuted once the the user has entered the correct deposit which is withnin the stipulated range. So it should be outside the while loop

Your full code should similar to this:

balance = 30000

choice = int(input("Enter 1 to Check Balance\nEnter 2 to deposit\nEnter 3 to withdrawl\n:"))
if choice == 1:
    print("Your balance is $",balance)
if choice == 2:
    while True:
        deposit = float(input("Enter the deposit you would like to make:"))
    
       
        if(deposit < 0 or deposit > 100000):          
            print("INVALID ENTRY! The deposit must not be less than 0 or greater than 100000.")
            continue
        else:
            break
    
        newbalance = deposit + balance
        print("Your balance is $", newbalance)
  • List item
0

Always check user input. You need to offer the user some way of exiting the program (menu option)

You may find this pattern instructive:

balance = 30_000
MAX = 100_000

while True:
    try:
        option = int(input('Enter 1 to check balance\nEnter 2 to deposit\nEnter 3 to withdraw\nEnter 9 to exit\n: '))
        if not option in {1, 2, 3, 9}:
            raise ValueError('Invalid option')
        if option == 9:
            break
        if option != 1:
            amount = float(input('Enter amount: '))
            if amount < 0 or amount > MAX:
                    raise ValueError(f'Amount cannot be negative of over {MAX}')
            if option == 2:
                balance += amount
            else:
                balance -= amount
        print(f'Current balance = {balance:.2f}')
    except ValueError as e:
        print(e)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22