简体   繁体   中英

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. 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.

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 this 1 , you'll need to rename stat.py to something else (though not to statistics , which would have the same problem).


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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