-2

I currently have this code

num_lines = int(input())
lines = []
tempy = ''
ctr = 1
abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
}
for i in range(0, num_lines):
  tempy = input()
  lines.append([])
  lines[i].append(tempy)


for o in range(0, num_lines):
  for p  in range(0, len(lines[o])):
    for u in range(0, len(lines[o][p])):
      if lines[o][p][u] in abc:
        lines = str(lines)
        if ctr % 2 == 0:
          print(lines[o][p][u])
          lines[o][p][u].upper()
        else:
          print(lines[o][p][u])
          lines[o][p][u].lower()
        ctr += 1

print(lines)

but the .upper line does not seem to have effect, can anyone please tell me why?

Thank you in advance, and if there is an answer already please kindly tell me that instead of marking as a duplicate cause I did search for a good hour

A Viper
  • 82
  • 1
  • 10
  • 1
    I have no idea what you're trying to do with your indexing... What do you mean by `lines[o][p][u]`? There aren't three nested lists? – 0TTT0 Oct 31 '17 at 01:27
  • 4
    what are you actually trying to accomplish with this code? – 0TTT0 Oct 31 '17 at 01:30
  • 1
    Please reduce your program to the shortest possible program that still demonstrates the error. If your question is about `.upper()`, that demonstration program should be about three lines long. See [mcve] for more information. – Robᵩ Oct 31 '17 at 01:50
  • 1
    I suspect your question could be demonstrated thus: `s = 'abcdef'` / `s.upper()` / `print(s)`, expected output `ABCDEF`, actual output `abcdef`. Is that so? – Robᵩ Oct 31 '17 at 01:53
  • 1
    strings are immutable. You have to assign the returned value from `upper()` to a variable, it will not be changed in place. So instead of `lines[o][p][u].upper()` do `lines[o][p][u] = lines[o][p][u].upper()`. Your example code is way too complex for the issue you are trying to show. You could make an example far more minimal than this. – Paul Rooney Oct 31 '17 at 02:00

1 Answers1

3

The .upper() and .lower() functions do not modify the original str. Per the documentation,

For .upper():

str.upper()

Return a copy of the string with all the cased characters converted to uppercase.

For .lower():

str.lower()

Return a copy of the string with all the cased characters converted to lowercase.

So if you want the uppercase and lowercase characters respectively, you need to print lines[o][p][u].upper() and lines[o][p][u].lower() as opposed to lines[o][p][u]. However, if I correctly understand your objective, from your code sample, it looks as though you're trying to alternate uppercase and lowercase characters from string inputs. You can do this much more easily using list comprehension with something like the following:

num_lines   = int(input("How many words do you want to enter?: "))
originals   = []
alternating = []

for i in range(num_lines):
        
    line = input("{}. Enter a word: ".format(i + 1))
    originals.append(line)
    alternating.append("".join([x.lower() if j % 2 == 0 else x.upper() for j, x in enumerate(line)]))

print("Originals:\t",   originals)
print("Alternating:\t", alternating)

With the following sample output:

How many words do you want to enter?: 3
1. Enter a word: Spam
2. Enter a word: ham
3. Enter a word: EGGS
Originals:       ['Spam', 'ham', 'EGGS']
Alternating:     ['sPaM', 'hAm', 'eGgS']
Community
  • 1
  • 1
Erick Shepherd
  • 1,403
  • 10
  • 19