Reputation: 27
I tried to run this program, but for some reason the string gets changed only to lowercase. The vowels don't turn to lowercase. Any ideas on why?
Thanks!
def changeCaps(string):
i = 0
while i < len(string):
if string[i] == 'a' or string[i] == 'e' or string[i] == 'i' or string[i] == 'o' or string[i] =='u':
print(string[i].upper())
i = i + 1
else:
print(string[i].lower())
i = i + 1
changeCaps("AlbErT")
Upvotes: 3
Views: 9181
Reputation: 123
What doubleo said in his two comments is correct: because the A and E in AlbErt are already capitalized, they aren't equal to lowercase a and e, and as such, they are made to be lowercase along with all the consonants. if you ware wanting to change the case of any letter typed, that would require a different routine. Something more along these lines:
def changeCaps(string):
i = 0
while i < len(string):
if string[i].islower():
print(string[i].upper())
i = i + 1
else:
print(string[i].lower())
i = i + 1
changeCaps("AlbErT")
This will result in any uppercase letters becoming lowercase and any lowercase letters becoming uppercase, and whether or not it is a vowel or consonant will have nothing to do with it.
Also, why not use a for loop instead? This will work just as well, and take up fewer lines of code:
def changeCaps(string):
for i in range(len(string)):
if string[i].islower():
print(string[i].upper())
else:
print(string[i].lower())
changeCaps("AlbErT")
Okay, so it only saves two lines, but it makes more sense to use a for loop in my opinion. Either way, the resultant output would be:
aLBeRt
On a final note, as Anton pointed out, you don't even really need a numeric pointer, just go over the string.
def changeCaps(string):
for c in string:
if c.islower():
print(c.upper())
else:
print(c.lower())
changeCaps("AlbErT")
(Thanks, Anton!)
Upvotes: 2