Issue
Trying to use try
and except
to accept strings only and display an error message if an int
is typed in.
This is my code:
name = input('Enter Your Name: ')
try:
s_name = str.lower(name)
except:
print('Please your Alphabets Only')
quit()
Solution
str.lower()
doesn't throw error when you pass a string of numbers. So, your try-except
is not working.
>>> str.lower('ASD123')
>>> 'asd123'
>>> str.lower('123')
>>> '123'
To get your desired output, you can use isalpha()
.
name = input('Enter Your Name: ')
if name.isalpha():
print(name) # or do whatever you want to do with name
else:
print('Please your Alphabets Only')
Answered By - BhusalC_Bipin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.