ModuleNotFoundError:没有名为'pythonds'的模块

时间:2017-02-21 06:32:09

标签: python

我想简单地为这些代码添加输入,但由于包pythonds,我不断收到此错误。

  

ModuleNotFoundError:没有名为'pythonds'的模块。

此错误让我无法完成,我不确定如何能够解决此错误。请提前帮助并表示感谢。

from pythonds.basic.stack import Stack

def infixToPostfix(infixexpr):
    prec = {}
    prec["^"] = 4
    prec["%"] = 3
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
               (prec[opStack.peek()] >= prec[token]):
                  postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList)

def postfixEval(postfixExpr):
    operandStack = Stack()
    tokenList = postfixExpr.split()

    for token in tokenList:
        if token in "0123456789":
            operandStack.push(int(token))
        else:
            operand2 = operandStack.pop()
            operand1 = operandStack.pop()
            result = doMath(token,operand1,operand2)
            operandStack.push(result)
    return operandStack.pop()

def doMath(op, op1, op2):
    if op == "*":
        return op1 * op2
    elif op == "/":
        return op1 / op2
    elif op == "+":
        return op1 + op2
    elif op == "^":
        return op1 ** op2
    elif op == "%":
        return op1 % op2
    else:
        return op1 - op2

string = input("Enter a string: ")

print(infixToPostfix(string))

1 个答案:

答案 0 :(得分:5)

确保pip install pythonds。我能够在Python提示符中使用该import语句而没有任何问题:

>>> from pythonds.basic.stack import Stack