python 初学者,但现在已经编程了大约 5 年。我怀疑我有很多东西要学习以面向对象的方式做事,但我知道基础知识。我计划编写一个计算器,以显示它对挑战的工作以及我将从中获得的知识。我刚开始,这就是我所拥有的,它对我来说真的很难看。你会怎么做?
PS这只是一个简单的脚本,用于从括号内取出问题,将其相加,展示工作,然后评估完整的问题。
import re
def EvalParenths(problem):
contents = ""
if re.match( "\(", problem):
contents = re.match("(\(.*\))", problem)
parenthsAnswer = contents.group(0)
problem = problem.replace(parenthsAnswer, '')
print " \ \n " + str(eval(parenthsAnswer)) + problem
problem = problem.replace(parenthsAnswer, '')
answer = eval(parenthsAnswer+problem)
print " \ \n " + str(answer)
else:
print "Didn't Find Parenthesis"
def ProblemHasParenths(problem):
return re.match( "\(", problem)
"""""
Example Problem: (12/4)*2
"""""
problem = raw_input()
if ProblemHasParenths:
EvalParenths(problem)