Need help with this assignment, python newbie here, 30 minutes till deadline and i couldn't figure out what's missing. my teacher said it won't work like this. updated,
if 0 < amount <= 20:
return 0.005, 0
if 20 < amount <= 50:
return 0.893, 0
elif 50 <= amount <= 100:
return 1.000, 0
elif 100 <= amount <= 200:
return 1.900, 0
elif 200 <= amount <= 300:
return 2.800, 0
elif 300 <= amount <= 500:
return 3.500, 0
elif 500 <= amount <= 700:
return 4.000, 0
elif 700 <= amount <= 1000:
return 4.750, 0
else:
return 5.500, 0
CodePudding user response:
I think your problem is here
def has_enough_balance(balance, amount, transaction_type):
return balance >= calculate_fee(amount, transaction_type) amount
calculate_fee() returns a tuple , in your has_enough_balance() function, you add the amount to it, you cant add a numeric and a tuple
index calculate_fee(amount, transaction_type)
to choose which variable you want to use there and your code should be fine
"depends on how and where the function should be used later, it's an abstract function " you could edit your has_enough_balance() function to
def has_enough_balance(balance, amount, transaction_type, fee_type):
index_dict = {"sender":0, "receiver":1}
return balance >= (calculate_fee(amount, transaction_type)[index_dict[fee_type]]) amount
basically since you have calculate_fee()
returning a tuple, you only want to use one of those values at a time.
you can follow the tuple with [*index of the value you want to use*]
to grab the correct value. so I added fee_type
as a argument for has_enough_balance()
and a dictionary inside to let you say whether you want to use your function for the sender fee or the receiver fee, and the dictionary will match it to its corresponding index, and [index_dict[fee_type]]
grabs the correct slice out of calculate_fee()
CodePudding user response:
Your calculate_fee
method returns two variables.
In python, when you return multiple variables that's a tuple (example: (0, 0)
)
But in this block of code
# Checking if client has enough balance before request is sent
def has_enough_balance(balance, amount, transaction_type):
return balance >= calculate_fee(amount, transaction_type) amount
You try to compare it to a single variable balance
and you also try to add it to a single variable amount
. These operations are not supported between a tuple, and an int.
From your comment i saw that calculate_fee
returns the fee for the sender and the fee for the receiver.
You could edit your has_enough_balance
function with a boolean parameter forSender
to know if you're calculating the amount for the sender or for the receiver.
You could then use the value of this parameter to know which of the two fees you need to add in your calculation.
# Checking if client has enough balance before request is sent
def has_enough_balance(balance, amount, transaction_type, forSender):
senderFee, receiverFee = calculate_fee(amount, transaction_type)
fee = senderfEE if forSender else receiverFee
return balance >= fee amount