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.')
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.')
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.