Reputation:
I created a function that turns every odd letter into lower case and every even letter to upper case but it does not what to work.I want it to return the string where all evens are lower and odds are upper
def myfunc(a):
b = a.split()
c = 1
for letter in b:
if c%2==0:
return letter.upper
else:
return letter.lower
c=c+1
return b
Upvotes: 0
Views: 222
Reputation: 79
You can do something like this
def myfunc(myString):
word = []
while a:
for i in range(len(myString)):
if i % 2 == 0:
word.append(myString[i].upper())
else:
word.append(myString[i].lower())
break
return ''.join(word)
Upvotes: 0
Reputation: 581
If you're interested in a one liner
def func(a):
return "".join([c.lower() if i%2 else c.upper() for i,c in enumerate(a)])
Upvotes: 1
Reputation: 11238
you use return
inside a function when some condition is met and once condition is met data preceding return is given back. so in your case either you should update the b
or create a new list and add data to it.
Below is a sample code implementation of your code
# your code goes here
def myfunc(a):
b = list(a)
for index, letter in enumerate(b):
if index%2:
b[index] = letter.lower()
else:
b[index] = letter.upper()
return ''.join(b)
print(myfunc("hello world")) # output HeLlO WoRlD
or
def myfunc(a):
b = list(a)
return ''.join([(value.lower() if index%2 else value.upper()) for index, value in enumerate(b)])
Upvotes: 0
Reputation: 1292
Any function will acknowledge only one return call. Whichever return it encounters first, will give the return value of function. Please try this.
def myfunc(a):
b = list(a)
for index, letter in enumerate(b):
if index % 2 == 0:
b[index] = letter.upper()
else:
b[index] = letter.lower()
return "".join(b)
Upvotes: 2