-
-
Save def-/70ef4afa22552b3ce20d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from strutils import replace, find, parseInt | |
echo "Input an operation:" | |
try: | |
let | |
line = stdin.readLine.replace(" ", "") | |
opindex = line.find({'+','-','*','/'}) | |
if opindex == -1: | |
raise newException(ValueError, "No known operand found") | |
let | |
op = line[opindex] | |
arg1 = line[0..opindex-1].parseInt | |
arg2 = line[opindex+1..^1].parseInt | |
result = case op | |
of '+': arg1 + arg2 | |
of '-': arg1 - arg2 | |
of '*': arg1 * arg2 | |
of '/': arg1 div arg2 | |
else: 0 | |
echo "Result = ", result | |
except: | |
echo "An error occured: ", getCurrentExceptionMsg() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment