+ 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)
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')
+ 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
+ 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)
+ 2
Thanks
+ 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")
+ 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)
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.
0
money_left = balance-to_cash if to_cash >=500 and to_cash< balance else "Error"