Reputation: 1
I want to have the program check if the code is or is not 'w', 'W', 'd' or 'D' and then direct the user from there, but as it is, it always goes to invalid() even when the input is 'w', 'W', 'd' or 'D'. And I don't want to use 'while code is equal to 'w', 'W', 'd' or 'D' continue on' because even though that works while the program is running, it causes the program to start over from the beginning after it's finished.
Plus, I'm not allowed to do the simple way using if statements
if code == 'W' or code == 'w' or code == 'D' or code == 'd':
prevbalance = float(input('Enter your previous balance: $'))
else:
invalid()
Here is the problem area:
code = input('Enter your transaction code: ')
while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
invalid()
prevbalance = float(input('Enter your previous balance: $'))
Here is the entire program if it helps:
def invalid():
input('Invalid transaction code. Please select another code.')
main()
def deposit(prevbalance, amount, code):
Wbalance = ''
Dbalance = prevbalance + amount
balance(Wbalance, Dbalance, code)
def withdrawal(prevbalance, amount, code):
Dbalance = ''
Wbalance = prevbalance - amount
if amount > prevbalance:
print('~~ERROR~~ You cannot withdrawal more than you have')
input(' Please try a lower amount back at the main menu')
main()
else:
balance(Wbalance, Dbalance, code)
def balance(Wbalance, Dbalance, code):
if code == "W" or code == "w":
print('Your new balance is: $',format(Wbalance, ',.2f'),)
else:
print('Your new balance is: $',format(Dbalance, ',.2f'),)
def main():
name = input('Enter your name: ')
ID = input('Enter your account ID: ')
code = input('Enter your transaction code: ')
while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
invalid()
prevbalance = float(input('Enter your previous balance: $'))
amount = float(input('Enter your transaction amount: $'))
if code == "W" or code == "w":
withdrawal(prevbalance, amount, code)
elif code == "D" or code == "d":
deposit(prevbalance, amount, code)
main()
input('Press ENTER to continue...')
Upvotes: 0
Views: 86
Reputation: 47860
not
doesn't distribute across all of your conditions. You need to add parentheses:
while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
Or better yet, use the in
operator:
while code not in ('W', 'w', 'D', 'd'):
Upvotes: 3
Reputation: 24100
This is an operator precedence problem. In the following:
while not code == 'W' or code == 'w' or code == 'D' or code == 'd':
the "not" operator binds more tightly than "or", so it is equivalent to:
while (not code == 'W') or code == 'w' or code == 'D' or code == 'd':
which is not what was intended. You can fix it with parentheses:
while not (code == 'W' or code == 'w' or code == 'D' or code == 'd'):
Or by using De Morgan's laws:
while code != 'W' and code != 'w' and code != 'D' and code != 'd':
But I suggest the following:
while code not in ('W', 'w', 'D', 'd'):
Hope that helps.
Upvotes: 2
Reputation: 5184
In your while condition not
will negate only code=='W'
not other conditions.
So you would probably need to do:
while code not in ('W', 'w', 'D', 'd'):
Upvotes: 2