Introduction..
Assume you are asked to code a program to accept the number of tennis grandslam titles from the user and process them. We already know, Federer and Nadal share the maximum grandslam titles in Tennis which is 20 [As of 2020] while the minimum is 0, lot of players are still fighting to get their first grandslam title.
Let us create a program to accept the titles.
Note - Execute the program from terminal.
Example
import argparse def get_args[]: """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, all arguments are strings. 2. type - The actual Python data type - [note the lack of quotes around str] 3. help - A brief description of the parameter for the usage """ parser = argparse.ArgumentParser[ description='Example for one positional arguments', formatter_class=argparse.ArgumentDefaultsHelpFormatter] # Adding our first argument player titles of type int parser.add_argument['titles', metavar='titles', type=int, help='GrandSlam Titles'] return parser.parse_args[] # define main def main[titles]: print[f" *** Player had won {titles} GrandSlam titles."] if __name__ == '__main__': args = get_args[] main[args.titles]
Output
Our program is now ready to accept the titles. So let us pass any number[not float] as argument.