Python TypeError: unsupported operand type(s) for /: 'str' and 'float'

前端 未结 4 1357
再見小時候
再見小時候 2020-12-18 16:50

My code:

total=tef+tpf-price

I\'ve got this error:

  total=tef+tpf-price
unsupported operand type(s) for -: \'float\' and \         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 17:38

    The only way that error could occur is if price is a string. Make price a float or an integer (depending on what you want) to fix the problem.

    Either this:

    tef=float(price)*5/100.0
    

    or this:

    tef=int(price)*5/100.0
    

    Notice that, in Python, to preform an operation between two objects, those object must be of the same type (and support the operation of course).

提交回复
热议问题