-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
executable file
·66 lines (45 loc) · 2.01 KB
/
calc.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import argparse
def add(numbers):
return sum(numbers)
def multiply(numbers):
result = 1
for n in numbers:
result *= n
return result
def divide(numbers):
if len(numbers) < 2:
raise ValueError("At least two numbers are required for division.")
if len(numbers) > 2:
raise ValueError("Cannot divide with more than two numbers.")
if numbers[1] == 0:
raise ValueError("Division by zero is not allowed.")
return numbers[0] / numbers[1]
def subtract(numbers):
result = numbers[0]
for n in numbers[1:]:
result -= n
return result
def main():
parser = argparse.ArgumentParser(description="A simple calculator with add and multiply commands.")
subparsers = parser.add_subparsers()
add_parser = subparsers.add_parser('add', help='Adds the provided numbers.')
add_parser.add_argument('numbers', type=int, nargs='+', help='The numbers to add.')
add_parser.set_defaults(func=add)
multiply_parser = subparsers.add_parser('multiply', help='Multiplies the provided numbers.')
multiply_parser.add_argument('numbers', type=int, nargs='+', help='The numbers to multiply.')
multiply_parser.set_defaults(func=multiply)
divide_parser = subparsers.add_parser('divide', help='Divides the first number by the second.')
divide_parser.add_argument('numbers', type=float, nargs=2, help='The numbers to divide, given as two numbers where the first is divided by the second.')
divide_parser.set_defaults(func=divide)
subtract_parser = subparsers.add_parser('subtract', help='Subtracts the provided numbers from the first number.')
subtract_parser.add_argument('numbers', type=int, nargs='+', help='The numbers to subtract, starting from the first number.')
subtract_parser.set_defaults(func=subtract)
args = parser.parse_args()
if hasattr(args, 'func'):
result = args.func(args.numbers)
print(result)
else:
parser.print_help()
if __name__ == '__main__':
main()