It's really simple code and I can try other ways but I really can't figure out what's specifically wrong with the logic of the code I've written.
def capitalize_letter(text, letter):
for x in text:
letter.upper()
return text
capitalize_letter('goku', 'O')
Your code doesn't actually change the original text
variable at all, it just returns exactly what you fed in.
for x in text:
will iterate through each character in text, which you could use to test if that character matches the letter you want to capitalise, but then you do nothing with x, making it useless.
Then you write letter.upper()
, but you never update letter from the 'O' fed in, so this line will just return 'O' each loop, but you don't assign it to anything/do anything with it, so it's pretty useless.
A potential way to change your code would be:
I would use an empty string to store the results in, like so:
def capitalize_letter(text, letter):
output_text = ''
Then your for loop is correct, and you can append each character in the text (x) to the output text string:
def capitalize_letter(text, letter):
output_text = ''
for x in text:
output_text += x
return output_text
This will return output_text
which will match text
. However, next you need to check if each character (x) matches the letter you want to capitalise, and if so, make it a capital letter, like so:
if x == letter:
x = x.upper()
Put all together, you get:
def capitalize_letter(text, letter):
output_text = ''
for x in text:
if x == letter:
x = x.upper()
output_text += x
return output_text
NOTE when you call this function, you should ensure letter
is lower case, as you want to change from lowercase to uppercase, not uppercase to uppercase.
e.g:
capitalize_letter('goku', 'o')
'gOku'