1

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. But the stat.py file won't let me use 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.

ron252
  • 21
  • 8
  • 2
    There is a builtin module called `stat` which is probably what you're getting when you do `import stat`. Try renaming your stat.py to something else that isn't a builtin and it should work. – match Dec 10 '21 at 21:44

1 Answers1

2

The module stat is builtin module that ships with 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.

To get around this1, you'll need to rename stat.py to something else (though not to statistics, which would have the same problem).


1Alternatively, 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.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43