-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathargs_multiply.py
More file actions
38 lines (24 loc) · 978 Bytes
/
args_multiply.py
File metadata and controls
38 lines (24 loc) · 978 Bytes
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
import sys
"""
This is just a basic example of command line arguments in python,
if you want you can use it in your own code and to execute this script
you must use "python args_multiply.py (arg1) (arg2)".
"""
def get_args():
args = sys.argv
# We don't need the first argument (name of the script)
args = args[1:]
# Checkes if there are only two arguments and they are digits
if len(args) == 2:
if args[0].isdigit() and args[1].isdigit():
# Return both as integers so we can multiply
return int(args[0]), int(args[1])
# If the arguments are not numbers raise an exception
else:
raise Exception("The arguments must be numbers")
# If there are more than two arguments raise an exception
else:
raise Exception("The number required for this program to work is 2, you put " + str(len(args)) + " arguments")
if __name__ == "__main__":
x, y = get_args()
print(x * y)