-
Notifications
You must be signed in to change notification settings - Fork 12
/
argparse_1.py
31 lines (24 loc) Β· 951 Bytes
/
argparse_1.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
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Simple calculator for basic arithmetic operations.')
# Adding positional arguments
parser.add_argument("number1", help='First number', type=int)
parser.add_argument("number2", help='Second number', type=int)
parser.add_argument("operation", help='Operation (add, subtract, multiply)', choices=["add", "subtract", "multiply"])
args = parser.parse_args()
print(f'Number 1: {args.number1}')
print(f'Number 2: {args.number2}')
print(f'Operation: {args.operation}')
n1 = args.number1
n2 = args.number2
result = None
if args.operation == 'add':
result = n1 + n2
elif args.operation == 'subtract':
result = n1 - n2
elif args.operation == 'multiply':
result = n1 * n2
else:
print(f'Invalid operation: {args.operation}')
exit(1)
print(f'Result: {result}')