我的临时计算器无法访问我制作的库,我该如何解决这个问题?

时间:2017-04-11 21:01:56

标签: python function

在python中,我有一个库,它包含计算器的临时函数作为学习项目。它工作得比较早,但现在主程序无法从库中获取函数或变量的定义。

主要可执行文件:

```print('Version 1.2.1 Beta')
print('Notice: Version 1.1.3 is unstable')
import maths
run = True
while run == True:
    print getFunction
    inLine = raw_input('>>> ')
    desFunction = inLine
    checkFunction()  

库:

    #Mathematical Symposium
#Begin Assignments
print('Version 1.1.4')
print('Converted to pylib for math functions')
print('Non-Executable')
print('Alternatively you can define desFunction as your function then execute checkFunction()')
getFunction = 'Please input desired function'
getX = 'Please input the first numerical value'
getY = 'Please input the second numerical value'
ans = 'Null'
run = True
#End Assignments
#Begin Definitions
def funcdir():
    functionli = ['add', 'subtract', 'multiply', 'divide', 'square root']
    print functionli
def checkFunction():
    if desFunction == 'add':
        add()
    elif desFunction == 'subtract':
        subtract()
    elif desFunction == 'multiply':
        multiply()
    elif desFunction == 'divide':
        divide()
    elif desFunction == 'square root':
        sqroot()
    elif desFunction == 'halt':
        run = False
    elif desFunction == 'help':
        funcdir()
def add():
    print getX
    inLine = raw_input('>>> ')
    x = inLine
    print getY
    inLine = raw_input('>>> ')
    y = inLine
    ans = float(x) + float(y)
    print ans
def subtract():
    print getX
    inLine = raw_input('>>> ')
    x = inLine
    print getY
    inLine = raw_input('>>> ')
    y = inLine
    ans = float(x) - float(y)
    print ans
def multiply():
    print getX
    inLine = raw_input('>>> ')
    x = inLine
    print getY
    inLine = raw_input('>>> ')
    y = inLine
    ans = float(x) * float(y)
    print ans
def divide():
    print getX
    inLine = raw_input('>>> ')
    x = inLine
    print getY
    inLine = raw_input('>>> ')
    y = inLine
    ans = float(x) / float(y)
    print ans
def sqroot():
    import math
    num = input('>>> ')
    print math.sqrt(num)
#End Definitions
#Begin Visible Process
#No Visible Process
#End Visible Process

如果有人知道如何解决这个问题,那将是一个很大的帮助。感谢。

2 个答案:

答案 0 :(得分:1)

导入“maths”后,它的函数和变量可以作为“maths.getFunction”等访问。

答案 1 :(得分:1)

导入模块只会将模块的名称添加到当前名称空间。它不能让您直接访问模块的内容。为此,您需要使用属性访问语法(例如print maths.getFunction),或使用from module import names显式导入某些名称(列出您想要的所有名称,或使用*导入一切)。您可能需要from maths import getFunction, checkFunction(虽然我不确定您的desFunction行应该做什么)。