-1

Created a program to calculate sales tax and create a list of the user input to sum to obtain the sales price. Loop should end when 0 is entered. I am getting the following error:

Traceback (most recent call last):
  File "/home/Strongheartedpr/4-4.py", line 28, in <module>
    price = sumList(list1, len(list1))
TypeError: object of type 'float' has no len()

Code:

print ("Sales Tax Calculator\n")

answer = "y"
while answer == "y":

    def calcState(price):
        state_tax = .06
        return price * state_tax

    def displayAnswer(price):

        state_tax_amt = calcState(price)


        print("Total: ",  price)
        print('Sales tax:   ', state_tax_amt)
        print('Total after tax: ', price + state_tax_amt )


    list1 = float(input('Enter the price of the purchase: '))

    def sumList(list, size):
        if (size == 0):
            return 0
        else:
            return list[size - 1] + sumList(list, size - 1)

    price = sumList(list1, len(list1))

    def main():

        displayAnswer(price)

    if __name__ == "__main__":
        main()
    answer = input("\nAgain? (y/n): ")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
CVYC CVYC
  • 75
  • 1
  • 8
  • 1
    What did you expect `list1` to be after `list1 = float(input('Enter the price of the purchase: '))`? – user2357112 Mar 14 '20 at 02:07
  • If you assign `list1 = float(...)`, then do `len(list1)` and are told a `float` has no `len`, can you *really* not understand your mistake? Read the error message, look at the code, and *think*. – Tom Karzes Mar 14 '20 at 02:33
  • Thank you for pointing out the obvious. I am confused on how to create the list and would like some assistance. – CVYC CVYC Mar 14 '20 at 02:36
  • Does this answer your question? [In Python, how do I convert all of the items in a list to floats?](https://stackoverflow.com/questions/1614236/in-python-how-do-i-convert-all-of-the-items-in-a-list-to-floats) – wwii Mar 14 '20 at 02:47

2 Answers2

0

Look at this line list1 = float(input('Enter the price of the purchase: ')).

The float() function will return a float, and you can't call len() on a float. You might want to think about ways to get a list from the user's input rather than just a single float.

jufer002
  • 128
  • 5
0

If you want to take the input as a float inside of a list, you can do

list1 = [float(input("Enter the price of the purchase: "))]

This way, you can take the len of list1 since it is actually a list.

WangGang
  • 533
  • 3
  • 15