1

On line 14, I want to notify the user that their account is overdrawn. I want this to appear when they have withdrawn up to 50 more than their current balance. If they go over this the withdrawal will be refused.

bal = input("What is your account balance?:            ")
intbal = int(bal)

withdraw = input("How much would you like to withdraw?:     ")
intwith = int(withdraw)

max = intbal + 50

if intwith <= intbal:

    print()
    print("Withdrawal successful. Current balance:   ", intbal - intwith)

elif intwith >= intbal or max:

    print()
    print("Withdrawal successful, the account is overdrawn. Current balance:        ", intbal - intwith)

elif intwith > max:

    print()
    print("Withdrawal refused, insufficient funds. Current balance:", intbal)

else:

    print("Invalid input")

I tried using "or" which clearly did not work. What are some solutions?

21010
  • 19
  • 3
  • Python can do chained greater-than/less-than comparisons: `elif intbal <= intwith <= max:` – Jared Smith Apr 12 '23 at 11:08
  • Also, welcome to Stack Overflow! Please take [the tour](https://stackoverflow.com/tour) and read [how to ask](https://stackoverflow.com/help/how-to-ask). This question is a pretty good first question but there are a fair amount of rules for posting on the site. – Jared Smith Apr 12 '23 at 11:11

2 Answers2

2

solution

In Python you can chain comparisons.

Change the line 14 to.

elif max >= intwith > intbal:

why your expression on line 14 always evaluates to True

If statement in python uses 'magic' __bool__() method (python documentation on __bool__()) to evaluate, whether the expression is True or False. For integer, this is True if int != 0. So your expression is equivalent to elif intwith >= intbal or max != 0:. As the max variable is not 0, it clearly always evaluates to True.

Vojtěch Chvojka
  • 378
  • 1
  • 15
1

The or statements combines two booleans. You are just saying or max, and since max (you should use another name since max is an inbuilt function) is not 0 it is seen as true. So your statement always reads as "x or True". Instead you should do:

elif intwith >= intbal and intwith <= max:
lotus
  • 116
  • 6