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
+ 2

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, 8:24 AM
Simisola Osinowo
Simisola Osinowo - avatar
8 Answers
+ 3
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')
19th Apr 2021, 8:46 AM
Amir
Amir - avatar
+ 3
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
19th Apr 2021, 9:06 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
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)
19th Apr 2021, 3:15 PM
Mr Rmp
Mr Rmp - avatar
+ 2
Thanks
19th Apr 2021, 9:23 AM
Simisola Osinowo
Simisola Osinowo - avatar
+ 1
left = 0 acct = int (input ("account total")) money = int(input ("withdrawl amount: ")) if money >= 500 and money <= acct: left = acct - money print (left) else: print ("error")
23rd Sep 2021, 7:51 PM
Devery Mcneely
Devery Mcneely - avatar
+ 1
this is the correct way: 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)
1st Jun 2022, 3:29 PM
Jhonatan Pereda Azabache
0
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.
20th Apr 2021, 5:19 PM
ボリス
0
money_left = balance-to_cash if to_cash >=500 and to_cash< balance else "Error"
11th Apr 2023, 8:56 AM
Wasim Al Mostafa
Wasim Al Mostafa - avatar