Reputation: 451
I am a very novice programmer and even more novice in Python. I am attempting to detect whether a character passed to a function is lower or upper case.
def encode(char, key, position, skipped):
if char.islower() == True:
ascii_offset = 97
if char.isupper() == True:
ascii_offset = 65
else:
print("You dun goofed laddie")
exit(2)
...
return char
The function exits in the 'else' statement. However, I am somewhat confident that the in fact characters are indeed passed to the function...
Upvotes: 1
Views: 120
Reputation: 778
@Tikhon wrote: "Thank you very much, that worked! I'm not too sure why... but I'm very grateful"
Explanation: If a character is lowercase, you execute the first if block. However, the execution of the code will proceed iteratively to the next if block, and because it is False (assuming the first was True) you execute the else case.
So using elif will implement the desired behavior:
if condition A:
do something
elif condition B:
do something else
else:
do some default case
Upvotes: 1
Reputation: 1363
Your issue is you have two if statements and the second has an else. You want something like:
def encode(char, key, position, skipped):
if char.islower():
offset = 97
elif char.isupper():
offset = 65
else:
print("Somethings wrong")
exit(2)
return char
The two if statements causes it to fail because if you send in a lower case letter it will always cause the else
statement to run. The second if
statement is saying, if this is not an upper case letter, exit(2)
. So any lower case letter will cause the exit. Also watch your indentations. Thats how python separates code blocks.
Upvotes: 3