-1
def bill_cal():
  split_amount = bill_amount_list * (1-cashback_rate) / person_split_no
  print(f"each person should pay {split_amount}.")

bill_amount = input("How much is the bill? You can enter multiple bills separated by comma.\n")
bill_amount_list = bill_amount.split(",")
cashback_rate = float(input("Is there any credit card cashback on the bills? 2% = 0.02\n"))
person_split_no = int(input("How many people are splitting the bill?"))
for bill in map(float, bill_amount_list):
  bill_cal()

I am trying to cast float on each item obtained from bill_amount.split(","), and perform the bill_cal() function, but got below error:

How much is the bill? You can enter multiple bills separarted by comma.
100,200
Is there any credit card cashback on the bills? 2% = 0.02
.02
How many people are splitting the bill?2
Traceback (most recent call last):
  File "main.py", line 10, in <module>
    bill_cal()
  File "main.py", line 2, in bill_cal
    split_amount = bill_amount_list * (1-cashback_rate) / person_split_no
TypeError: can't multiply sequence by non-int of type 'float'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [How do I multiply each element in a list by a number?](https://stackoverflow.com/questions/35166633/how-do-i-multiply-each-element-in-a-list-by-a-number) – mkrieger1 Oct 02 '22 at 07:48

2 Answers2

0

You are "multiplying" your bill_amount_list which is... well, a list. Sequences can be "multiplied" by int (meaning you will repeat the sequence), but not by float, hence your error.

Pass your bill to the function, and use it in the computation:

def bill_cal(amount):
    split_amount = amount * (1-cashback_rate) / person_split_no
#...
for bill in map(float,bill_amount_list):
  bill_cal(bill)
gimix
  • 3,431
  • 2
  • 5
  • 21
  • Thank you! I tried your code and it works. Now I want to add the sum of the split_amount, I tried math.fsum but it returns that TypeError: 'float' object is not iterable, is there another way of adding the sum of the amount? – pingusama Oct 02 '22 at 08:12
  • Do you want the total for each person? Sum the amounts in your list and call `bill_cal` one last time with that amount – gimix Oct 02 '22 at 09:04
  • yes it should be total for each person. I am not sure how do I get the list of split_amount? – pingusama Oct 02 '22 at 09:13
  • You almost have it already: `sum(map(float,bill_amount_list))` – gimix Oct 02 '22 at 09:20
-1

You need to call the function with a local variable. Also it's good practice to check your input as well like this:

if item.isnumeric():
    'call the function'
Der Esel
  • 19
  • 2