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)
5 ответов
+ 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)
+ 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"
+ 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)
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)?
0
Ok.good