0

I'm trying to make a program that will convert any text into a different form. That means that a text such as 'hi there' becomes 'hI tHeRe'.

list = []

word = input('Enter in a word or a sentence! ')

for num in range(len(word)):
    list.clear()
    list.append('i')
    letter = word[num]
    for x in range(len(list)):
        if x % 2 == 0:
            i = word.index(letter)
            place = letter.lower()
            word = word.replace(word[i], place)
        if not x % 2 == 0:
            i = word.index(letter)
            place = letter.upper()
            word = word.replace(word[i], place)

print(word)

However, when I run the code it just prints the same string as normal.

qwerteee
  • 197
  • 9
  • Why do you have nested loops? What is the idea behind that? `num` is going through each index in your string, and then for each `num`, `x` is also going through each index in your string, and you're using `x`'s evenness to decide how to replace the letter at `num`. I can't figure out how you intended for this to work. – khelwood Jul 29 '20 at 21:47
  • My suggestion: don't use `replace`. It's the wrong approach for this. Convert your string to a list and iterate through it once, updating each index in turn. Then convert back to a string at the end. – khelwood Jul 29 '20 at 21:51

4 Answers4

3

When using replace, you have to assign the result to your variable:

word = word.replace(word[i], place)

However, replace is actually not what you want here. replace replaces all instances of a certain pattern with a new string. In your current code, every instance of whatever letter word[i] represents will be replaced with the result of .lower() or .upper().

You also don't want to use the word list, since doing so will shadow the Python built-in list class.

If you want to keep most of your original logic, you can follow @khelwood's suggestion in the comments and end up with the following:

word = input('Enter in a word or a sentence! ')
wordList = list(word)

for i in range(len(word)):
    if i % 2 == 0:
        wordList[i] = word[i].lower()
    else:
        wordList[i] = word[i].upper()

print(''.join(wordList))
jdaz
  • 5,964
  • 2
  • 22
  • 34
0

Here is one of my previous codes, you can change all the variable names to whatever you see fit.

s = input('Enter in a word or string.')
ret = ""
i = True  # capitalize
for char in s:
    if i:
        ret += char.upper()
    else:
        ret += char.lower()
    if char != ' ':
        i = not i
print(ret)

I hope it works for you.

Rebecca Bibye
  • 190
  • 2
  • 18
0

Try this one liner -

a = 'hi there'
''.join([i[1].lower() if i[0]%2==0 else i[1].upper() for i in enumerate(a)])
'hI ThErE'

If you care about each word starting from lowercase then this nested list comprehension works -

' '.join([''.join([j[1].lower() if j[0]%2==0 else j[1].upper() for j in enumerate(i)]) for i in a.split()])
'hI tHeRe'
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
0

The problem is with list.clear in the beginning of the for loop. Each iteration you clear the list so the second for iteration run on the first item only. Remove list.clear and it should scan the input word

Ronen
  • 189
  • 3