gpt4 book ai didi

python - 模块未找到错误 : No module named 'pythonds'

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:01 24 4
gpt4 key购买 nike

我试图简单地向这些代码添加输入,但由于软件包 pythonds,我不断收到此错误。

ModuleNotFoundError: No module named '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))

最佳答案

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

>>> from pythonds.basic.stack import Stack

关于python - 模块未找到错误 : No module named 'pythonds' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com