【问题标题】:Can not figure out what is going wrong with this coding无法弄清楚这个编码出了什么问题
【发布时间】:2015-12-31 08:44:11
【问题描述】:

我试图弄清楚为什么我在 python 中没有得到这个编码的正确答案。到目前为止,这是我的代码:

def main():
    base = input('Enter an integer for the base: ')
    exponent = input('Enter an integer for the exponent: ')
    print(base,'to the power', exponent,'equals', power)

def power(base, exponent):
    if exponent <= 1:
       return base
    else:
       return base * power(base, exponent - 1)
main()

当我使用 2 和 5(底数、指数)运行程序时,我得到了:

Enter an integer for the base: 2
Enter an integer for the exponent: 5
2 to the power 5 equals <function power at 0x03DDC300>
>>> 

我的问题是:为什么我得到“0x03DDC300 处的功能电源”或类似答案,而不是正确答案 32?

【问题讨论】:

  • print(power)
  • 调用函数:power()。仅使用 power 只会给出名称。 "function power at 0x..." 就是这样,名字:一个名为 power 的函数,地址在 0x...

标签: python


【解决方案1】:

当你有一个没有括号的函数时,返回值会告诉你这个函数,而不是返回值。为了获得返回值,您必须分别为函数power() 插入参数、基数和指数。

此外,当一个函数使用另一个函数时——比如函数 1 使用函数 2——你应该先定义函数 2,而不是相反。

这应该可行:

def power(base, exponent): if exponent <= 1: return base else: return base * power(base, exponent - 1) def main(): base = input('Enter an integer for the base: ') exponent = input('Enter an integer for the exponent: ') print(base,'to the power', exponent,'equals', power(base, exponent)) main()

【讨论】:

    【解决方案2】:

    您需要使用正确的整数参数调用函数power 以获得正确的输出。

    print(base,'to the power', exponent,'equals', power(int(base), int(exponent))) # call the function `power`
    

    没有这个,power 只会返回一个可调用对象。

    In [1]: def some_func():
       ...:     return 2 
       ...: 
    
    In [2]: print some_func # print the function without calling 
    <function some_func at 0x601a050> # returns a callable
    
    In [3]: print some_func() # call the function
    2
    

    【讨论】:

    • 函数power()需要两个参数。
    • 感谢 Rahul Gupta 和 Kevin Guan。将 power(int(base), int(exponent))) 添加到代码中就可以了。感谢您的帮助。
    【解决方案3】:

    因为power()是一个函数,所以你需要调用它,而不仅仅是打印它。
    而你的函数power()需要两个参数,所以你可以试试下面的代码:

    def main():
        base = int(input('Enter an integer for the base: '))
        exponent = int(input('Enter an integer for the exponent: '))
        # use `int()` function if you wish user enter a intege
    
    
        print(base,'to the power', exponent,'equals', power(base, exponent))
    
    def power(base, exponent):
        if exponent <= 1:
           return base
        else:
           return base * power(base, exponent - 1)
    
    main()
    

    演示:

    Enter an integer for the base: 10
    Enter an integer for the exponent: 20
    10 to the power 20 equals 100000000000000000000
    

    【讨论】:

      【解决方案4】:
      print(base,'to the power', exponent,'equals', power)
      

      看线。你不是在调用函数,而是在写函数名。

      您需要调用该函数。

      power 更改为power(base,exponent)

      例如,如果你想计算 2 的 3 次方,把上面的行改成这样:

      print(base,'to the power', exponent,'equals', power(2,3)) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        相关资源
        最近更新 更多