File: adder.py

package info (click to toggle)
python-friendly-traceback 0.7.62%2Bgit20240811.d7dbff6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,264 kB
  • sloc: python: 21,500; makefile: 4
file content (18 lines) | stat: -rw-r--r-- 467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Demonstration of a program that uses command line arguments
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("numbers", nargs="*", help="List of numbers to add.")
parser.add_argument("--to_int", help="Converts the sum to integer", action="store_true")


total = 0
args = parser.parse_args()

for number in args.numbers:
    total += float(number)

if int(total) == total and args.to_int:
    total = int(total)

print(f"The sum is {total}.")