如何使这段 Python 代码更可用和可读?
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)
Beginner in python, but been programming for about 5 years now. I suspect I have a lot to learn about doing things the object oriented way, but I know the basics. I planned on programming a calculator that shows it's work for the challenge and knowledge i'll gain from it. I just started and this is what i've got, and it just looks really ugly to me. How would you have done it differently?
P.S. This is just a simple script to take the problem from inside parenthesis, add it up, show the work, then evaluate the full problem.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些问题:
当给定输入
(1+2)/(3+4)
时,它将尝试评估1+2)/(3+4
。它也不会一直进入嵌套括号,为此您需要使用递归,
我认为您应该在“查看答案”之前再次尝试。
Some problems:
When it's given the input
(1+2)/(3+4)
, it's going to try to evaluate1+2)/(3+4
.It also doesn't go all the way into nested parentheses, for this you would need to use recursion.
I think you should make another attempt at this before you "look at the answers".
如果您想制作一个简单的计算器,您可以尝试实现Shunting-yard 算法。
但是,如果您想使用正则表达式方法,我仍然会做一些不同的事情:
它的工作原理与您的函数并不完全相同,但您可以轻松地对函数进行修改以匹配我的函数。正如您所看到的,我还添加了很多注释来解释一些内容。
If you want to make a simple calculator you could try implementing Shunting-yard algorithm.
But if you want to go with regex approach I'd still do it a little differently:
It doesn't exactly work the same as your function, but you could easily implement modifications into your function to match mine. As you can see, I've also added a lot of comments to explain some stuff.
我可能会将出现的 替换
为
在
您不检查内容是否匹配的情况下,因此如果您将输入“(1”传递给它,那么当您尝试评估内容时会出现异常。group(0)
Don在真实的程序中并不是每个都使用
eval
!您可以使用 pyparsing 来制作一个完整的解析器,但我认为每个人都应该自己尝试至少一次作为练习!
I'd probably replace occurrences of
with
In
you don't check to see whether contents matches or not so if you pass it the input "(1" you'll get an exception when you try to evaluate contents.group(0)
Don't every use
eval
in a real program!You could use pyparsing to make a full parser, but I think it is the kind of thing everyone should try on their own as an exercise at least once!
为什么不只匹配双括号和匹配括号?单个
(
的第一个匹配并不是真正必要的,因为如果双打的匹配失败,这意味着没有表达式可供您评估。此外,括号选择算法可以稍微改进嵌套括号等
Why don't you match only for the double and matching parentheses? The first match for a single
(
is not really necessary because if the match for the doubles fail, this means there are no expression for you to evaluate.Also, the parentheses selection algorithm could be improved a bit for nested parens etc.