Skip to main content
Code Review

Return to Answer

Typo
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

The in operator checks inif op is one of the elements of the set of 4 supported operators. If it is, we compute answer based on the exact value of op, and then print out the solved expression at the end. If op isn’t a supported operator, we print "Invalid Operator".

The in operator checks in op is one of the elements of the set of 4 supported operators. If it is, we compute answer based on the exact value of op, and then print out the solved expression at the end. If op isn’t a supported operator, we print "Invalid Operator".

The in operator checks if op is one of the elements of the set of 4 supported operators. If it is, we compute answer based on the exact value of op, and then print out the solved expression at the end. If op isn’t a supported operator, we print "Invalid Operator".

Grammar
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

Unlike the previous code, we explicitly test elif op == "*":, which is more consistent. If the op doesn’t match any of the defined operatoroperators, we execute the else: statement, which raises an Exception, which left uncaught will terminate the program without executing the following print statement.

Unlike the previous code, we explicitly test elif op == "*":, which is more consistent. If the op doesn’t match any of the defined operator, we execute the else: statement, which raises an Exception, which left uncaught will terminate the program without executing the following print statement.

Unlike the previous code, we explicitly test elif op == "*":, which is more consistent. If the op doesn’t match any of the defined operators, we execute the else: statement, which raises an Exception, which left uncaught will terminate the program without executing the following print statement.

Accidentally dropped `*` from `\s*`
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103

I think ([-+]?\d+)\s*([-+/*])\s\s*([-+]?\d+) does the trick, but it looks like gobblygook.

import operator, re
OPERATIONS = {
 "+": operator.add,
 "-": operator.sub,
 "/": operator.truediv,
 "*": operator.mul,
 }
problem = input(" Enter Your Math Problem ")
if m := re.fullmatch(r"([-+]?\d+)\s*([-+/*])\s\s*([-+]?\d+)", problem):
 x = int(m[1])
 op = m[2]
 y = int(m[3])
 answer = OPERATIONS[op](x, y)
 print(f"{x} {op} {y} = {answer}")
else:
 print(f"Invalid or unsupported math problem: {problem}")

I think ([-+]?\d+)\s*([-+/*])\s([-+]?\d+) does the trick, but it looks like gobblygook.

import operator, re
OPERATIONS = {
 "+": operator.add,
 "-": operator.sub,
 "/": operator.truediv,
 "*": operator.mul,
 }
problem = input(" Enter Your Math Problem ")
if m := re.fullmatch(r"([-+]?\d+)\s*([-+/*])\s([-+]?\d+)", problem):
 x = int(m[1])
 op = m[2]
 y = int(m[3])
 answer = OPERATIONS[op](x, y)
 print(f"{x} {op} {y} = {answer}")
else:
 print(f"Invalid or unsupported math problem: {problem}")

I think ([-+]?\d+)\s*([-+/*])\s*([-+]?\d+) does the trick, but it looks like gobblygook.

import operator, re
OPERATIONS = {
 "+": operator.add,
 "-": operator.sub,
 "/": operator.truediv,
 "*": operator.mul,
 }
problem = input(" Enter Your Math Problem ")
if m := re.fullmatch(r"([-+]?\d+)\s*([-+/*])\s*([-+]?\d+)", problem):
 x = int(m[1])
 op = m[2]
 y = int(m[3])
 answer = OPERATIONS[op](x, y)
 print(f"{x} {op} {y} = {answer}")
else:
 print(f"Invalid or unsupported math problem: {problem}")
Source Link
AJNeufeld
  • 35.2k
  • 5
  • 41
  • 103
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /