Hi all,

New to these forums, i've just started learning python, and for my class i have one question that i have to do that i understand but aint to sure about what exactly is required.

"Design, code and test a comp program that reads a character entered from the keyboard and then determines whether the character is a letter (i.e. a-z, A-Z). If the character is a letter, the program also determines whether it is uppercase or lowercase.
eg. Output wud be "Char entered is upper/lower case letter " or "Char is not alphabetic".

I have come up with this, but when i enter in a upper case letter it is just reading it as a lower case. I thought python was case sensitive?

x = "a" or "b" or "c" or "d" or "e" or "f" or "g" or "h" or "i" or "j" or "k" or "l" or "m" or "o" or "n" or "q" or "r" or "s" or "t" or "u" or "v" or "w" or "x" or "y" or "z" or "A" or "B" or "C" or "D" or "E" or "F" or "G" or "H" or "I" or "J" or "K" or "L" or "M" or "O" or "P" or "Q" or "R" or "S" or "T" or "U" or "V" or "W" or "X" or "Y" or "Z"
x = raw_input("Enter a keyboard character: ")
if x is "a" or "b" or "c" or "d" or "e" or "f" or "g" or "h" or "i" or "j" or "k" or "l" or "m" or "o" or "n" or "q" or "r" or "s" or "t" or "u" or "v" or "w" or "x" or "y" or "z":
    print 'Character entered is an lower case letter'
elif x is "A" or "B" or "C" or "D" or "E" or "F" or "G" or "H" or "I" or "J" or "K" or "L" or "M" or "O" or "P" or "Q" or "R" or "S" or "T" or "U" or "V" or "W" or "X" or "Y" or "Z":
    print 'Character entered is an upper case letter'
else:
    print 'Character is not alphabetic'
print "program complete"

Cheers

There's much easier ways to accomplish what you're trying to do. Try using python's built-in string module.

if type(x) == str:
  if x.isupper():
    # character is uppercase
  elif x.islower():
    # character is lowercase

else:
  # this is not a string
Member Avatar for leegeorg07

yes it is if you use the string function (below) e.g.

b = "d"
b.isupper() # will return false
b.islower() # will return true

this is something i came up with in 5 mins that does it quicker and is easier to read:

def checker():
	b = raw_input("enter a char")
	if b.isalpha():
		print "%s, is a char" % b
		if b.isupper():
			print "%s, is upper" % b
		else:
			print "%s, is lower" % b
	elif b.isdigit():
		print "%s, is a digit" % b

thanks all for the replies, i thought their would be a easy module to use ;)

yes it is if you use the string function (below) e.g.

b = "d"
b.isupper() # will return false
b.islower() # will return true

this is something i came up with in 5 mins that does it quicker and is easier to read:

def checker():
	b = raw_input("enter a char")
	if b.isalpha():
		print "%s, is a char" % b
		if b.isupper():
			print "%s, is upper" % b
		else:
			print "%s, is lower" % b
	elif b.isdigit():
		print "%s, is a digit" % b

Why are you using tabs for indents?

Member Avatar for leegeorg07

i wasnt, i was using Scite that auto indents, sorry if you had to re-indent it

This line doesn't make any sense.

x = "a" or "b" or "c" or "d" or "e" or "f" or "g" or "h" or "i" or "j" or "k" or "l" or "m" or "o" or "n" or "q" or "r" or "s" or "t" or "u" or "v" or "w" or "x" or "y" or "z" or "A" or "B" or "C" or "D" or "E" or "F" or "G" or "H" or "I" or "J" or "K" or "L" or "M" or "O" or "P" or "Q" or "R" or "S" or "T" or "U" or "V" or "W" or "X" or "Y" or "Z"

For ruture reference, if you want to stick with your original idea, do not use "is 'a'"; it would be

x = raw_input("Enter a keyboard character: ")
if x in ["a", "b", "c", ...etc]

But as already posted, Python does most of this for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.