Reputation: 761
I'm trying to write a program that determines if a character is uppercase, lowercase, digit, or non-alphanumeric without string methods like isupper, islower, isdigit. The program is that everything I enter, it's telling me that its a lower case letter. Can someone help me out?
character = input("Enter a character: ")
lowerLetters = "abcdefghijklmnopqrstuvwxyz"
upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits = "0123456789"
lowerCount = 0
upperCount = 0
digitCount = 0
nonAlphaCount = 0
for ch in character:
for ch in lowerLetters:
lowerCount += 1
for ch in upperLetters:
upperCount += 1
for ch in digits:
digitCount += 1
else:
nonAlphaCount += 1
if lowerCount > 0:
print(character, "is a lower case letter.")
elif upperCount > 0:
print(character, "is an upper case letter.")
elif digitCount > 0:
print(character, "is a digit.")
elif nonAlphaCount > 0:
print(character, "is a non-alphanumeric character.")
Upvotes: 3
Views: 13705
Reputation: 107287
The problem is with following part of your code :
for ch in character:
for ch in lowerLetters:
lowerCount += 1
for ch in upperLetters:
upperCount += 1
for ch in digits:
digitCount += 1
else:
nonAlphaCount += 1
You increase the variables without any condition you need to use all
function for this job :
character = input("Enter a character: ")
lowerLetters = "abcdefghijklmnopqrstuvwxyz"
upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digits = "0123456789"
if all(ch in character ckafor ch in lowerLetters):
print(character, "is a lower case letter.")
elif all(ch in character for ch in upperLetters):
print(character, "is an upper case letter.")
elif all(ch in characterfor ch in digits):
print(character, "is a digit.")
else:
print(character, "is a non-alphanumeric character.")
Now if you are looking for another way you can use regex for this task but is not as efficient as the preceding solutions.
character = input("Enter a character: ")
import re
if re.match(r'[a-z]+',characters):
print(character, "is a lower case letter.")
elif re.match(r'[A-Z]',characters):
print(character, "is an upper case letter.")
elif re.match(r'[0-9]+',characters):
print(character, "is a digit.")
else:
print(character, "is a non-alphanumeric character.")
Upvotes: 1
Reputation: 3384
Your code is good (though not the best one for your purpose even without using the methods you mentioned) but you have a couple of typos :) This is what you have:
for ch in character:
for ch in lowerLetters:
lowerCount += 1
for ch in upperLetters:
upperCount += 1
for ch in digits:
digitCount += 1
else:
nonAlphaCount += 1
And this is want you wanted to type:
for ch in character:
if ch in lowerLetters:
lowerCount += 1
elif ch in upperLetters:
upperCount += 1
elif ch in digits:
digitCount += 1
else:
nonAlphaCount += 1
Upvotes: 3
Reputation: 11134
You can use ascii value
Numbers 0-9 ASCII 48 to 57
Lowercase letters a-z ASCII 97 to 122
Uppercase letters A-Z ASCII 65-90
use ord
function. like this:
>>ord('a')
97
So, to check if a
is a lowercase letter or not, do:
if 97<=ord('a')<=122:
print "lowercase character"
Upvotes: 4