Python Function Code Error

前端 未结 2 1277
广开言路
广开言路 2021-01-25 16:05

I am trying to build a code that spits out y given f(x). Apparently, mine isn\'t working. Can you please help?

def f(n):
    \'\'\'The Function\'\'\'
    return          


        
相关标签:
2条回答
  • 2021-01-25 16:45

    Use this:

    return (-5*(n**5))+(69.0*(n**2))-47
    

    The algebraic notation you are using i.e. omitting the '*' sign causes python to think that you are trying to make a function call:

    69.0(n**2)  # python thinks 69.0 is a function name and n**2 is the parameter of this call
    

    That is why the '*' operator is necessary, between two operands.

    0 讨论(0)
  • 2021-01-25 16:49

    I think the problem is that you're using algebraic notation where (I presume) you want to multiply. For instance, instead of:

    -5(n**5)
    

    you want

    -5 * (n ** 5)
    

    A single asterisk ("*") is multiplication in Python (and many other programming languages).

    If you just put parentheses directly after something, Python is interpreting that as you attempting to call that thing. It might be more obvious with named variables:

    a = 5
    a(n ** 5)
    

    Is line 2 a function call, or multiplication? In Python, it's unambiguously the latter, but you can't call integers, so you get an exception like TypeError: 'int' object is not callable

    0 讨论(0)
提交回复
热议问题