【问题标题】:Imported function not working?导入的功能不起作用?
【发布时间】:2017-10-30 00:34:41
【问题描述】:

在一个模块中,我有一个功能,例如:

def check_answer(num1, num2, ans, op):
    int(num1)
    int(num2)
    int(ans)
    if op == "-":
        return num1 - num2 == ans
    if op == "+":
        return num1 + num2 == ans

在一个单独的模块中。我测试了这个功能。

import myfunctions

myfunctions.check_answer(1,1,2,"+")

它通常返回一个真/假值。但是现在我已经导入了它。它什么都不返回?

【问题讨论】:

  • 请修正缩进
  • 导入失败了吗? (在这种情况下,您应该会遇到某种错误。)
  • 你认为int(num1) 在做什么?你的意思是:num1 = int(num1)。此外,您的意思是:print(myfunctions.check_answer(1,1,2,"+")) - 您可能会得到正确的结果,只是您从未将其打印到屏幕上

标签: python function return python-import


【解决方案1】:

除非您遇到导入错误,否则它看起来会返回一个值,但您似乎没有对它做任何事情。尝试打印:

import myfunctions

print(myfunctions.check_answer(1, 1, 2, "+"))

输出应该是True

【讨论】:

    【解决方案2】:

    你可以先这样试试:

    import myfunctions
    print(myfunctions)
    

    如果您没有看到异常,则可能证明您导入成功。然后,尝试像这样打印您的结果:

    print(myfunction.check_answer(1,1,2,"+"))
    

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2018-03-01
      • 2023-01-30
      相关资源
      最近更新 更多