11 Replies - 7316 Views - Last Post: 12 July 2012 - 06:49 PM Rate Topic: -----

#1 mike316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-July 12

how to fix this unsupported operand type for *: function and float

Post icon  Posted 08 July 2012 - 07:15 AM

I made this calculator where you enter the cost of five items. It adds up the subtotal, and it asks, 'Enter the amount of tax.' I enter in .06 for 6 percent. Next I want to get the tax total for the items. When I run the program I keep getting this error , "unsupported operand type(s) for *: 'function' and 'float" I don't understand why I can't multiply the subtotal by the tax in the def tax_total(subtotal,tax): section. I tried a search online and nothing seems to work. Can someone please try to explain to me how to fix this, I would really appreciate it, I'm new to python and just trying to learn something new for my own enjoyment. Thanks for helping.
def main():
    item_one = input('Enter the cost of item one: ')
    item_two = input('Enter the cost of item two: ')
    item_three = input('Enter the cost of item three: ')
    item_four = input('Enter the cost of item four: ')
    item_five = input('Enter the cost of item five: ')
    subtotal(item_one,item_two,item_three,item_four,item_five)
    tax = input('Enter the amount of tax for the five items: ')
    tax_total(subtotal,tax)

def subtotal(item_one,item_two,item_three,item_four,item_five):
    subtotal = item_one + item_two + item_three + item_four + item_five
    print 'The subtotal is ' ,subtotal , 'dollars '

def tax_total(subtotal,tax):
    tax_total = subtotal * tax
    print 'The tax total is ' , subtotal
    
    
main()

This post has been edited by atraub: 12 July 2012 - 06:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to fix this unsupported operand type for *: function and float

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: how to fix this unsupported operand type for *: function and float

Posted 08 July 2012 - 07:20 AM

Well, subtotal IS a function, and not a float! Perhaps what you want to do is RETURN the subtotal from the subtotal function, assign it to a variable, and then pass that variable to the tax_total function? Maybe add a separate function to print the subtotal message.

By the way, rather than using five separate variables for the items, consider using a list.
Was This Post Helpful? 2
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: how to fix this unsupported operand type for *: function and float

Posted 08 July 2012 - 07:22 AM

In your earlier thread I suggested to you that you should replace your print statements with return statements, so that you can reuse the values you calculate in your functions in other functions.

As long as you don't do that, you can't reuse the results like you're trying to do here. If a function has a return statement, you can store its result in a variable and/or pass it as an argument to another function. As long as it doesn't, you can't.

This post has been edited by sepp2k: 08 July 2012 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: how to fix this unsupported operand type for *: function and float

Posted 11 July 2012 - 07:57 PM

Heres what I got sir.
#!/usr/bin/python3
def main():
    items = int(input('How many items in total did you purchase? :'))
    list1 = []
    for x in range(items):
        price = float(input('Price: '))
        list1.append(price)
    total = sum(list1)
    print (total)
    tax_total(total)
    return total
        
def tax_total(value):
    request = float(input('Tax: '))
    taxandtotal = value * request
    print (taxandtotal)
    return taxandtotal
                    
main()



Hope I helped.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 10:38 AM

View Postk3y, on 11 July 2012 - 10:57 PM, said:

Heres what I got sir.
#!/usr/bin/python3
def main():
    items = int(input('How many items in total did you purchase? :'))
    list1 = []
    for x in range(items):
        price = float(input('Price: '))
        list1.append(price)
    total = sum(list1)
    print (total)
    tax_total(total)
    return total
        
def tax_total(value):
    request = float(input('Tax: '))
    taxandtotal = value * request
    print (taxandtotal)
    return taxandtotal
                    
main()



Hope I helped.


Just handing someone code, with no explanation as to what it does and not addressing their question, never helps.

This post has been edited by atraub: 12 July 2012 - 10:39 AM

Was This Post Helpful? 2
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,149
  • Joined: 19-March 11

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 10:41 AM

Give a man the code, and he'll have his homework done for the day. Teach a man to code and he'll work for a lifetime.
Was This Post Helpful? 1
  • +
  • -

#7 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 03:22 PM

Quote

Just handing someone code, with no explanation as to what it does and not addressing their question, never helps.


I apologize let me go line by line for him:
(using comments)
#!/usr/bin/python3
def main(): # define function as 'main()'
    items = int(input('How many items in total did you purchase? :')) # request 'int' data type input and assign to identifier 'items'
    list1 = [] # create an empty list 
    for x in range(items): # for each value (x) within the range of the number inputted by user do the following
        price = float(input('Price: ')) # request the price of a product with 'float' data type (meaning it can take decimals)
        list1.append(price) # then add each of thoose prices to the list 
    total = sum(list1) # now lets add all of them togeter using the b.i.f 'sum' and assign identifier total to that value
    print (total) # lets print the value for the user
    tax_total(total) # next lets pass the argument total to the 'tax_total(value) function' (total will replace (value))
    return total # next lets return total (making it so we can use this value in other fucntions of our code
        
def tax_total(value): # define function as 'tax_total(value)' ~ value being any argument we pass to it (total)
    request = float(input('Tax: ')) # now lets request the specifed tax for thoose items
    taxandtotal = value * request # lets recieve the product, by multiplying the specific tax and (value) aka 'total'
    print (taxandtotal) # lets print that for the user
    return taxandtotal # return the 'taxandtotal' so we can use it in another function (not needed but better safe then sorry)
                    
main() # call main function

'''layout of code'''
# first: call main function
# second: main function calls tax_total and passes argument
# third: tax_total(value) does everything we need to know


Was This Post Helpful? 1
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 318
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 04:08 PM

You made a mistake. Your comments aren't explaining anything at all. Ok, for example, we "pass the argument total to the 'tax_total(value) function'" - WHY do we do this? It doesn't help in understanding.

Comments clarify the code and give your reasons for writing it like that, to aid the developer - not to repeat what is already written in a different way. If you can't justify your design decisions, maybe it's a bad choice? I believe that comments that don't add anything new, it's better to leave them out.
Was This Post Helpful? 1
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 06:29 PM

Simown hit the nail on the head. Adding comments to every single line only accomplishes adding clutter and ultimately making the code more confusing. Instead, you can simply give an overview of the code or a simple pseudo-code walkthrough. I tend to write comments whenever scope changes, which means I write one at the top of every condition, every loop, every function, etc...

I think you don't understand what DreamInCode is all about. We are not a code writing service. People ask us questions and we help them learn the answers. We don't simply do their homework for them and hope it helps. We ask them the write questions, we talk about pseudo-code, we push people in the right direction. Our goal isn't to solve people's problems, our goal is to teach them how to solve the problem. How can giving someone code accomplish that?

This post has been edited by atraub: 12 July 2012 - 06:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 06:39 PM

View Postatraub, on 12 July 2012 - 08:29 PM, said:

Simown hit the nail on the head. Adding comments to every single line only accomplishes adding clutter and ultimately making the code more confusing. Instead, you can simply give an overview of the code or a simple pseudo-code walkthrough. I tend to write comments whenever scope changes, which means I write one at the top of every condition, every loop, every function, etc...

I think you don't understand what DreamInCode is all about. We are not a code writing service. People ask us questions and we help them learn the answers. We don't simply do their homework for them and hope it helps. We ask them the write questions, we talk about pseudo-code, we push people in the right direction. Our goal isn't to solve people's problems, our goal is to teach them how to solve the problem. How can giving someone code accomplish that?


I just posted my input because I thought it would help OP see that there are multiple ways to do the same thing. Thanks to OP he made me research/learn how to make that code I posted. I just posted my code because I learn from reading other people's code and seeing how things work through trial and error. I didn't mean to break some rule, I just pasted code in hopes that OP would reasearch what's within the code and learn something (meaning improve on my sloppy code).
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,149
  • Joined: 19-March 11

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 06:43 PM

Quote

I apologize let me go line by line for him:


Thanks. I appreciate your taking the time to do that, I really do.

A few minor points about your comments:

Quote

# request 'int' data type input and assign to identifier 'items'
# request the price of a product with 'float' data type (meaning it can take decimals)


This is not exactly correct. input() returns anything the user chooses to type in, as a string. The int() and float() functions attempt to convert their arguments into ints or floats. If those arguments do not represent ints or floats, the functions will fail with ValueError. So it's not that it asks for input as a float, instead it's asking for input and trying to make it into a float.

Quote

# now lets add all of them togeter using the b.i.f 'sum' and assign identifier total to that value


This is not the usual way of saying this, but I quite like it. Usually, one says "assign that value to the variable called total", which is correct, but you have to understand what it means (and think about left-hand values and right-hand values and whatnot) or you get confused. This way is nice, because it captures that sense that you're naming a value, not putting a value into a box.


tax_total(total)


Here we have a problem.

Because you don't capture the return value, total is not changed. This means that we're now moving ahead with the pre-tax value. The correct line here would be



total = tax_total(total)


Since we presumably want the result returned to be the total after taxes.


Also, it's usually considered good design to separate your data input from your calculations. You want to be able to do the calculations the same way no matter where the input comes from. As you've written your tax_total method, if you wanted to read the data from a file or expand this with a GUI you'd have to re-write the calculation method, since as it's written it can only be used in a keyboard-input context.

"Best practice" is to gather the values for tax rate and cost of individual goods, and then pass those values to methods which do the work for you.

Like so:

def main():
  # get a list of prices as prices_list - preferably in a method
  # get a tax rate as tax_rate  - again, encapsulate units of function into methods

  # post_tax = calculate_total_with_tax(prices_list, tax_rate)  - do the calculation
  # report(post_tax, prices_list, tax_rate)       - report back to the user
  #                                        (if you're doing accounting, be ready to itemize!)





The methods are left as an exercise for the student.


So, a few quibbles, but thanks for taking the trouble to explain your work. Next time, just give the explanations, and leave the work for the student! :)

Quote

I just pasted code in hopes that OP would reasearch what's within the code and learn something


This never happens. Nice idea, but it doesn't happen.

EDIT: one of the advantages of just giving high-level suggestions is that nobody sees the mistakes in your code! :) If you do give code examples, it's a really good idea to run them before posting...

This post has been edited by jon.kiparsky: 12 July 2012 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#12 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: how to fix this unsupported operand type for *: function and float

Posted 12 July 2012 - 06:49 PM

@jon.kiparsky
I didn't see any errors on my idle when I ran the code so I figured that it was all good. But thank you for all of that information, im sort of an informal coder I guess (lol). But I will definitely try to take your suggestions.
thank you
-k3y
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1