In: Computer Science
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist.
Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same.
Examples:
swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER'
swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I HOPE MY CAPS LOCK DOES NOT GET STUCK ON'
#code:
def swapCaps(val):
res="" #res string to store altered string
for i in val: #iterarte through each char
if i.isupper(): #if present char is upper
case
res=res+i.lower() #make it lower
case can concate it to result string
elif i.islower():#if present char is lower
case
res=res+i.upper() #make it upper
case can concate it to result string
elif i==' ': #if present char is space
res=res+i #conbcate it as it
is
return res #return result
#driver program to test
val='Hope you are all enjoying October'
print(swapCaps(val))
print(swapCaps( 'i hope my caps lock does not get stuck on' ) )
#error:
Failed tests
Symbols
If you completed the first two but failed this one you probably did not account for symbols being in the sentence. They should remain unchanged. The test was "H@lloween W!ll Sc@re You @s Much @s This Test C@se!" should result in "h@LLOWEEN w!LL sC@RE yOU @S mUCH @S tHIS tEST c@SE!"
More info
Traceback (most recent call last): File "/home/runner/unit_tests.py", line 24, in test_Symbols self.assertEquals(swapCaps("H@lloween W!ll Sc@re You @s Much @s This Test C@se!"), "h@LLOWEEN w!LL sC@RE yOU @S mUCH @S tHIS tEST c@SE!") AssertionError: 'hLLOWEEN wLL sCRE yOU S mUCH S tHIS tEST cSE' != 'h@LLOWEEN w!LL sC@RE yOU @S mUCH @S tHIS tEST c@SE!' - hLLOWEEN wLL sCRE yOU S mUCH S tHIS tEST cSE + h@LLOWEEN w!LL sC@RE yOU @S mUCH @S tHIS tEST c@SE! ? + + + + + + +
Changed code:
def swapCaps(val):
res="" #res string to store altered string
for i in val: #iterarte through each char
if i.isalpha(): # check if character is alphabet or not
if i.isupper(): #if present char is upper case
res=res+i.lower() #make it lower case can concate it to result string
else: #else the present char is lower case
res=res+i.upper() #make it upper case can concate it to result string
else: # if character is not alphabet then we directly append it to our result
res=res+i
return res #return result
#driver program to test
val='Hope you are all enjoying October'
print(swapCaps(val))
print(swapCaps( 'i hope my caps lock does not get stuck on' ) )
print(swapCaps( 'H@lloween W!ll Sc@re You @s Much @s This Test C@se!' ) )
Screenshot of the code to get the ordering which is important in python:
Output:
Explaination:
The main change in the code is if i.isalpha() which is a function is python which return true if the string/character is only alphabets. In this case we just used it for checking characters and not string as whole.
So if i.isalpha() is true then we check if i.isupper() which checks if the character is uppercase or not. If it is uppercase then we change it to lowercase and append to the result. If the character is not uppercase then we move to else. The reason for changing elif is there is no use for elif. Firstly we have added a if which check if character is alphabet or not so if it is an alphabet then it can only be uppercase or lowercase. So if uppercase is not true then it will be lowercase and then we will change it to uppercase.
In the main else part of i.isalpha() we will directly append it to the result. The contents can be special characters like @, ! or anything also it can be numbers like 1,2.. and it can also be a space which is also a character