Write a program to accept a name from a user. Raise and handle the appropriate exception(s) if the text entered by the user contains digits and/or special characters.

by

Last updated on Nov 5, 2022
Python Practicals (Question 13)

Input

name = input('enter your name \n')
name.split()
c = 0
s = '"[@_!#$%^&*()<>?/\|}{~:]"'
for i in range(len(name)):
    if name[i] in s or name[i].isdigit():
        c += 1
if c == 0:
    print("your name is " + name)
else:
    try:
        raise Exception('Invalid character')
    except Exception as e:
        print('invalid character in your name', e

Output

for valid character

enter your name 
aman
your name is aman

for invalid character

enter your name 
*aman
invalid character in your name Invalid character

How useful was this post?

5 star mean very useful & 1 star means not useful at all.

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Tags: