Reputation: 39
I am making a python function that gets a parameter of a character and then prints if the character is a uppercase, lowercase, a number or a special character. I get this error from Pylint
invalid syntax (, line 529)pylint(syntax-error)
#Finding if a number is a lowercase, uppercase, number or a special character
def check(charcater):
character_ascii = ord(character)
if character_ascii >= 97 and <= 122:
print('The character',character,'is a lowercase letter')
elif character_ascii >= 65 and <= 90:
print('The character',character,'is a uppercase letter')
elif character_ascii >= 48 and <= 57:
print('The character',character,'is a number')
else :
print('The character',character,'is a special character')
I get the error here
if character_ascii >= 97 and <= 122:
^
Please help me solve this error. Thanks in advance
Upvotes: 1
Views: 225
Reputation: 532113
You have two separate comparison expressions, joined by and
, and each one requires two operands.
if character_ascii >= 97 and character_ascii <= 122:
However, comparison operators allow for chaining, which allows you to include multiple comparisons in a single expression. The above is equivalent to
if 97 <= character_ascii <= 122:
In general, x op1 y op2 z
is equivalent to x op1 y and y op2 z
for any two comparison operators op1
and op2
. Comparison operators are
<
, >
, <=
, >=
==
, !=
is
, is not
in
not in
Upvotes: 1
Reputation: 937
comparison operators need a value on both sides so try changing them to this:
def check(charcater):
character_ascii = ord(character)
if character_ascii >= 97 and character_ascii <= 122:
print('The character',character,'is a lowercase letter')
elif character_ascii >= 65 and character_ascii <= 90:
print('The character',character,'is a uppercase letter')
elif character_ascii >= 48 and character_ascii <= 57:
print('The character',character,'is a number')
else :
print('The character',character,'is a special character')
Upvotes: 0