Back to main page
summaryrefslogtreecommitdiff
path: root/main.py
blob: dc99e146b95b0f183b23126b3c5b9b36417251c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
ops = "+-*/^"

def calc(a, b, op):
    if op == '+':
        return a + b
    elif op == '-':
        return a - b
    elif op == '*':
        return a * b
    elif op == '/':
        return a / b
    elif op == '^':
        return a ** b

help_menu = "'exit' - quit\n'help' - print this menu\nInput two numbers and an operator separated by spaces.\nExample input:  A + B\nAvailable operators: + - * / ^"
print(help_menu)

while True:
    instr = input("> ")
    if instr == "exit":
        break
    elif instr == "help":
        print(help_menu)
        continue
    args = instr.split()
    
    if len(args) < 3:
        print("Not enough arguments to perform operation.")
        continue

    try:
        args[0] = float(args[0])
    except:
        print("Invalid first argument.")
        continue
    
    try:
        args[2] = float(args[2])
    except:
        print("Invalid second argument.")
        continue
    
    if args[1] in ops:
        print(calc(args[0], args[2], args[1]))
    else:
        print("Invalid operation.")