Trying to solve ternary operator in python core
How do i add the last condition You are given a program for a bank card withdrawal system: it takes the account and the amount that the user wants to withdraw, then outputs the remaining money. If the requested cash is greater than the balance, the program outputs "Error". The bank wants to set a minimal value of $500 for withdrawal. Modernize the program so that it will output the same "Error" if the requested money is less than $500. Sample Input 4500 300 Sample Output Error balance = int(input()) to_cash = int(input()) #change the code money_left = balance-to_cash if to_cash<=balance else "Error" print(money_left)
4/19/2021 8:24:45 AMSimisola Osinowo
5 AnswersNew Answer
pri=int(input("Enter balance")) wtc=int(input("Enter amount to withdraw")) bal=pri - wtc msg= bal if wtc>=500 and bal>=0 else "Error" print(msg)
balance = int(input()) to_cash = int(input()) #change the if balance - to_cash >=0: if to_cash >=500: balance = balance - to_cash print(balance ) else: print("Error") else: print('Error')
Simisola Osinowo Flip it around, then use a chained and statement for the condition Psuesdocode ml = error if balance < to_cash < 500 else balance - to_cash
balance = int(input()) to_cash = int(input()) #change the code if to_cash >= 500 and to_cash <= balance: money_left = balance - to_cash print(money_left) else: print("Error") --- Though, this is simpler with only one error condition, but I think that one error message for each specific error would be better for debugging.