-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
41 lines (30 loc) · 891 Bytes
/
hello.py
File metadata and controls
41 lines (30 loc) · 891 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
39
40
from typing import Union
Number = Union[int, float]
def multiply(a: Number, b: Number) -> Number:
"""Return the product of a and b."""
return a * b
def main() -> None:
"""Print a greeting and demo the multiply function.
If two numbers are provided as CLI args, multiply them; otherwise use 3 and 4.
"""
import sys
print("Hello, World!")
def parse_num(s: str) -> Number:
try:
return int(s)
except ValueError:
return float(s)
args = sys.argv[1:]
if len(args) >= 2:
try:
a = parse_num(args[0])
b = parse_num(args[1])
except ValueError:
print("Usage: python hello.py [a b] # a and b must be numbers")
return
else:
a, b = 3, 4
result = multiply(a, b)
print(f"{a} * {b} = {result}")
if __name__ == "__main__":
main()