0

Is there a better way to do this?

I want the code to detect numbers and special characters and print:

"numbers not allowed" / "special characters not allowed"

while True:
try:
    x = input('Enter an alphabet:')
except ValueError:
    print('sorry i do not understand that')
    continue
if x in ('1', '2','3','4','5','6','7','8','9','0'):
    print('Numbers not allowed')
    continue
else:
    break
if x in ('a', 'e', 'i', 'o', 'u'):
print ('{} is a vowel'.format(x))

elif x in ('A', 'E', 'I', 'O', 'U'):
print ('{} is a vowel in CAPS'.fotmat(x))
else:
print('{} is a consonant'.format(x))
jpp
  • 159,742
  • 34
  • 281
  • 339
gannicus4431
  • 1
  • 1
  • 1
  • 1
    That first try-except seems pointless... and what exactly are you trying to validate? Only letters and spaces? – cs95 Mar 30 '18 at 17:07
  • You can `import string` and check to see if `x in string.ascii_letters` and for numbers check if `x in string.digits` – pault Mar 30 '18 at 17:07
  • You should the a look at https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – pault Mar 31 '18 at 14:08

3 Answers3

1

One way is to use the string library.

Here is some psuedocode, which assumes that the input is one character at a time:

import string
x = input('Enter an alphabet:')
if x in string.digits:
    print('Numbers not allowed')
elif x not in string.ascii_letters:
    print('Not a letter')

string.ascii_letters is a string that contains all the uppercase and lowercase letters:

print(string.ascii_letters)
#'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Likewise, string.digits is a string that contains all of the digits:

print(string.digits)
#'0123456789'
pault
  • 41,343
  • 15
  • 107
  • 149
  • This is the best solution. But as an alternative you can always `raise ValurError` manually instead of just using `print`. – jpp Mar 30 '18 at 21:03
0

You could do it a couple of ways but the pythonic method seems to me to be like so:

if any(char in string.punctuation for char in x) or any(char.isdigit() for char in x):
    print("nope")
Dylan Moore
  • 443
  • 4
  • 14
  • Why call `set`? `string.punctuation` already only contains unique values. – pault Mar 30 '18 at 17:14
  • because you can easily add something like .replace() if you want to allow specific chars. – Dylan Moore Mar 30 '18 at 17:19
  • I'm not following your logic. In this case `any(char in set(string.punctuation) for char in x)` and `any(char in string.punctuation for char in x)` produce the same output. In what case would it be different? – pault Mar 30 '18 at 17:22
  • You are correct, I was thinking of something else at the time. I should not switch from node projects to python questions. – Dylan Moore Mar 30 '18 at 17:59
0

May be this code does the job.

while True:
x = ord(input('Enter an alphabet:')[0])
if x in range(ord('0'), ord('9')):
    print('Numbers not allowed')
    continue
if x not in range(ord('A'), ord('z')):
    print('Symbols not allowed')
    continue
if chr(x) in 'aeiou':
    print('{} is a vowel'.format(chr(x)))
elif chr(x) in 'AEIOU':
    print('{} is a vowel in CAPS'.format(chr(x)))
else:
    print('{} is a consonant'.format(chr(x)))
continue

We select numbers, deselect whatever character except letters and then does the job.

miimote
  • 373
  • 2
  • 12