Trying to solve ternary operator in python core: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Trying to solve ternary operator in python core:

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)

19th Apr 2021, 6:09 PM
SHREYA
5 Answers
+ 5
balance = int(input()) to_cash = int(input()) money_left = balance-to_cash if to_cash<=balance and to_cash >= 500 else "Error" print(money_left)
8th Jun 2022, 3:05 PM
Julian Castellanos Dulcey
Julian Castellanos Dulcey - avatar
+ 3
The beauty of python is that it's very easy to read and understand. You want money_left to be (balance-to_cash) if to_cash is in between 500 and balance, else "Errror" That easly translates to: money_left = balance-to_cash if 500<=to_cash<=balance else "Error"
19th Apr 2021, 7:40 PM
Angelo
Angelo - avatar
+ 1
balance = int(input()) to_cash = int(input()) #change the code money_left = balance-to_cash if to_cash<=balance and to_cash>= 500 else "Error" print(money_left)
16th Nov 2022, 5:58 AM
Mostafa
Mostafa - avatar
0
The given code has only one condition so far, (to_cash<=balance). You need to join it with another condition that checks whether to_cash is greater than or equal to 500. Think about how the two conditions should be joined. Should the money be withdrawn if either condition is true (logical or), or if both conditions must be true (logical and)?
19th Apr 2021, 7:36 PM
Brian
Brian - avatar
0
Ok.good
15th Sep 2022, 3:22 PM
Mothe Saikrishna
Mothe Saikrishna - avatar