简体   繁体   English

为什么我的进口 function 不能在 main 中工作?

[英]Why won't my imported function work in main?

Here is the code I'm trying to import to get it working for a project.这是我尝试导入以使其适用于项目的代码。

import random

def ReturnHit(denominator,numerator):

  if (denominator == 0):
    denominator = 1
  if (numerator == 0):
    numerator = 1

  num = random.randint(1,denominator)
 #if num is less than a the numberator then return yes on the hit
  if (num < numerator):
    return True
  else:
    return False


def ReturnApproxCurvedHit(average,lowerLimit,upperLimit):
  #return true or false based on probability position in the bell curve
  n = range(lowerLimit,upperLimit)
  for i in n:
    if(ReturnHit(1,abs(average - i)*2)):
      return True
    else:
      return False

Then there's my main where I set up functions to test if I'm importing everything right.然后是我的主要设置功能以测试我是否正确导入了所有内容。 My test functions work perfectly fine even returning everything perfectly.我的测试功能工作得很好,甚至可以完美地返回所有内容。 The test function says 'hello' and returns '0' of course.测试 function 说“你好”,当然返回“0”。 But the stat.py file won't let me use ReturnHit.但是 stat.py 文件不允许我使用 ReturnHit。

#I need to find a way to test the stat.py
import stat
import module


module.say_hello()
z = module.zero()

print(z)

stat.ReturnHit(1,10)

It spits out an error the goes:它吐出一个错误:

Traceback (most recent call last):
  File "main.py", line 11, in <module>
    stat.ReturnHit(1,10)
AttributeError: module 'stat' has no attribute 'ReturnHit'

I'm on replit btw.顺便说一句,我正在重新制作。

The module stat is builtin module that ships with Python.模块stat是 Python 附带的内置模块。 Since builtin modules are searched first when resolving imports, it's that module that you're getting when you write import stat , not your local stat.py module.由于在解析导入时首先搜索内置模块,因此当您编写import stat时,您将获得该模块,而不是您的本地stat.py模块。

To get around this 1 , you'll need to rename stat.py to something else (though not to statistics , which would have the same problem).要解决这个问题1 ,您需要将stat.py重命名为其他名称(尽管不是statistics ,这将有同样的问题)。


1 Alternatively, you can circumvent the default module search path and import your stat.py module directly from its file path , but that's not a common or recommended practice. 1或者,您可以绕过默认模块搜索路径并直接从其文件路径导入您的stat.py模块,但这不是常见或推荐的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM