Argparse python

Hi Everyone,
I need help fixing of this code:
import argparse

def main():
parser = argparse.ArgumentParser()
parser.add_argument(‘name’, help=‘Enter your name’)
parser.add_argument(‘age’, type=int, help=‘Enter your age’)

args = parser.parse_args()

if type(args.name) == int:
    print('Argument can't be a number')

print(f'Hello, {args.name}!')
if args.age:
    print(f'You are {args.age} old.')

if name == ‘main’:
main()

Code have to check and end work if:

  1. Name is the second agument, not first.
  2. Name is a number

That isn’t going to work, because no matter what you type in at the command line, argparse is going to treat it as a str. What you can do is:

>>> name = '12'
>>> name.isnumeric()
True

Thank’s for help,
I have been solved this task
import argparse
import sys

def main():
parser = argparse.ArgumentParser()
parser.add_argument(‘name’, help=‘Enter your name’)
parser.add_argument(‘age’, help=‘Enter your age’)

args = parser.parse_args()

try:
    n = int(args.name)
    if n:
        print("Name can't be a number")
        sys.exit(1)
except ValueError:
    pass

print(f'Hello, {args.name}!')
if args.age:
    print(f'You are {args.age} years old.')

if name == ‘main’:
main()

You can specify that it needs to be a string like this:

parser.add_argument(‘name’, help=‘Enter your name’, type=str)

See argparse — Parser for command-line options, arguments and sub-commands — Python 3.12.5 documentation for more details

This won’t check to make sure it’s a non-numeric string but I also would imagine that somewhere in this world there is someone with a numeral in their name (John Smith, 3rd for example) who might get annoyed if you check for numerals in names.