patternpythonMinor
A prefix notation calculator that asks about everything step by step
Viewed 0 times
stepnotationthatabouteverythingprefixcalculatorasks
Problem
I made a prefix notation calculator.
Prefix notation, aka Polish notation, is a form of notation for logic, arithmetic and algebra, like infix and postfix (reverse Polish) notation.
The expression '+ 3 4' in prefix notation is as valid as '3 + 4' in infix notation, as well as '3 4 +' in postfix notation.
The calculator asks for one value per step, and asks about everything step by step.
```
"""
Pfxcalc
Programmed by zyabin101
This code is hereby released into the public domain
"""
import ast
def lit_input(prompt):
"""
Safely evaluate input. Only returns the following Python literal
structures: strings, numbers, tuples, lists, dicts, booleans, and 'None'.
Raises ValueError for malformed strings.
"""
return ast.literal_eval(raw_input(prompt))
def lorr_input(prompt, result):
inputted = raw_input(prompt)
return result if inputted == "result" else ast.literal_eval(inputted)
# Initialization
x = None
# Print welcome message
print "Welcome to Pfxcalc. Type 'help' for more information."
# Main program loop
while True:
# Get user input
cmd = raw_input(">> ")
# Process input
if cmd == "quit":
break
elif cmd == "result":
print x
elif cmd == "help":
helpmsg = """
Pfxcalc is a prefix notation calculator.
Prefix notation, aka Polish notation, is a form of notation for logic,
arithmetic and algebra, like infix and postfix (reverse Polish)
notation.
The expression '+ 3 4' in prefix notation is as valid as '3 + 4' in
infix notation, as well as '3 4 +' in postfix notation.
Every computation starts with entering a command and the result is
printed and saved in a variable that can be recalled by entering
'result'. The variable can even be used as an argument for functions,
using the same command.
List of commands:
quit - self explanatory
result - recall the result variable
help - p
Prefix notation, aka Polish notation, is a form of notation for logic, arithmetic and algebra, like infix and postfix (reverse Polish) notation.
The expression '+ 3 4' in prefix notation is as valid as '3 + 4' in infix notation, as well as '3 4 +' in postfix notation.
The calculator asks for one value per step, and asks about everything step by step.
```
"""
Pfxcalc
Programmed by zyabin101
This code is hereby released into the public domain
"""
import ast
def lit_input(prompt):
"""
Safely evaluate input. Only returns the following Python literal
structures: strings, numbers, tuples, lists, dicts, booleans, and 'None'.
Raises ValueError for malformed strings.
"""
return ast.literal_eval(raw_input(prompt))
def lorr_input(prompt, result):
inputted = raw_input(prompt)
return result if inputted == "result" else ast.literal_eval(inputted)
# Initialization
x = None
# Print welcome message
print "Welcome to Pfxcalc. Type 'help' for more information."
# Main program loop
while True:
# Get user input
cmd = raw_input(">> ")
# Process input
if cmd == "quit":
break
elif cmd == "result":
print x
elif cmd == "help":
helpmsg = """
Pfxcalc is a prefix notation calculator.
Prefix notation, aka Polish notation, is a form of notation for logic,
arithmetic and algebra, like infix and postfix (reverse Polish)
notation.
The expression '+ 3 4' in prefix notation is as valid as '3 + 4' in
infix notation, as well as '3 4 +' in postfix notation.
Every computation starts with entering a command and the result is
printed and saved in a variable that can be recalled by entering
'result'. The variable can even be used as an argument for functions,
using the same command.
List of commands:
quit - self explanatory
result - recall the result variable
help - p
Solution
Use a shebang at the beginning
This is something like
Remove some of the unnecessary comments
Comments are good. Excessive comments are just hard to read.
e.g.
Turn that into:
And:
Into:
Empty lines for readability
Using the previous example:
Into:
This just checks if you are running this directly, or from a module import/something else.
Maybe put the while loop into a function called
Don't assign unnecessary variables
The
The
Either use it, or remove it. Maybe make a variation of the docstring for the
Other notes:
-
Nice job with the ast safe eval, but a method that doesn't involve eval would also be nice. This is a lot of work, probably unnecessary.
-
I would set
-
I'm not sure, but you appear to have limited to 87 chars per line (or under). That is a PEP guideline, good job.
This is something like
#!/usr/bin/env python2, and helps interpreters know how to run it.Remove some of the unnecessary comments
Comments are good. Excessive comments are just hard to read.
e.g.
elif cmd == "+":
x = (lorr_input("Left side addend? ", x) +
lorr_input("Right side addend? ", x))
print x # Implicitly output resultTurn that into:
elif cmd == "+":
x = (lorr_input("Left side addend? ", x) +
lorr_input("Right side addend? ", x))
print xAnd:
# Get user input
cmd = raw_input(">> ")
# Process input
if cmd == "quit":
....Into:
cmd = raw_input(">> ")
# Process user input
if cmd == "quit":
....Empty lines for readability
Using the previous example:
cmd = raw_input(">> ")
# Process user input
if cmd == "quit":
....Into:
cmd = raw_input(">> ")
# Process user input
if cmd == "quit":
....if __name__ == "__main__" checkThis just checks if you are running this directly, or from a module import/something else.
Maybe put the while loop into a function called
start() and use:if __name__ == "__main__":
start()Don't assign unnecessary variables
The
helpmsg var is pretty much useless, just print the plain string.The
lit_input function is not usedEither use it, or remove it. Maybe make a variation of the docstring for the
lorr_input?Other notes:
-
Nice job with the ast safe eval, but a method that doesn't involve eval would also be nice. This is a lot of work, probably unnecessary.
-
I would set
x to 0 at the beginning, but that's personal preference.-
I'm not sure, but you appear to have limited to 87 chars per line (or under). That is a PEP guideline, good job.
Code Snippets
elif cmd == "+":
x = (lorr_input("Left side addend? ", x) +
lorr_input("Right side addend? ", x))
print x # Implicitly output resultelif cmd == "+":
x = (lorr_input("Left side addend? ", x) +
lorr_input("Right side addend? ", x))
print x# Get user input
cmd = raw_input(">> ")
# Process input
if cmd == "quit":
....cmd = raw_input(">> ")
# Process user input
if cmd == "quit":
....cmd = raw_input(">> ")
# Process user input
if cmd == "quit":
....Context
StackExchange Code Review Q#132394, answer score: 8
Revisions (0)
No revisions yet.