Andy
Andy

Reputation: 11

Why can't I initialize a variable to the indices of two other variables working on a string?

The wording of my question was not polite to the search feature on the site, so I apologize should someone feel this is a duplicate question, but I must ask anyway.

Working in Python 3.6.1, my goal is to find a substring of letters in a string that are in alphabetical order and if that substring of letters is the longest substring of letters in alphabetical order (aa would be considered alphabetical order), then print out the string. I have not gotten entirely close to the solution but I'm making progress; however, this came up and I'm confounded by it being completely new to Python. My question is, why is this valid:

s = 'hijkkpdgijklmnopqqrs'
n = len(s)
i = 0
a = 0
for i in range(n-2):
    if s[i] <= s[i+1]:
       a = s[i+1]
       i = s[i+2]
       a = i + a
print(a)

And yet this is not:

s = 'hijkkpdgijklmnopqqrs'
n = len(s)
i = 0
a = 0
b = ''
for i in range(n-2):
    if s[i] <= s[i+1]:
       b = a + i
       a = s[i+1]
       i = s[i+2]
       a = a + i
print(b)

When the latter code is run, I receive the error:

Traceback (most recent call last):
File "C:\Users\spect\Desktop\newjackcity.py", line 14, in <module>
b = a + i
TypeError: must be str, not int

What I am ultimately trying to do is to 'index in' to the string s, compare the zeroth element to the zeroth+1 element and if s[I] < s[I+1], I want to concatenate the two into my variable a for later printing. Because when I do this, a only prints out two letters in the string. I thought, well initialize the variable first so that a and i can be incremented, then added into a for comparison purposes, and b for printing.

I see now that I'm only going through n-2 iterations (in order to compare the second to last letter to n-1 so the logic is flawed, but I still don't understand the error of why all of a sudden binding a+i to a variable b will produce a str/int error? In my view saying s[i]; etc. is pulling out the elements as a string and this to me is proven in the fact if I run the first set of code, I get the output:

sr
>>>

Upvotes: 0

Views: 45

Answers (1)

Thomas Nelson
Thomas Nelson

Reputation: 683

In both for loops, you use i as the loop variable, so it starts as an int.

In the first version, you reassign i to a string, then add.

for i in range(n-2):
  # here i is an int, something between 0 and n-2
  if s[i] <= s[i+1]:
    a = s[i+1]  # a is a string...
    i = s[i+2]  # now you change i to a string
    a = i + a  # string + string: OK!

In the second version you try to add i first:

for i in range(n-2):
  # here i is an int, something between 0 and n-2
  if s[i] <= s[i+1]:
     b = a + i # string + int, can't do it...
     a = s[i+1]
     i = s[i+2]
     a = a + i

You will have an easier time debugging your code if you pick more meaningful names.

edit: here is my cleaned up version of your code:

s = 'hijkkpdgijklmnopqqrs'
# i = 0 isn't needed, range starts at 0
# the first character is always 'alphabetical'
alph_substr = s[0]
# range(1,n) is [1,2, ..., n-1]
for i in range(1, len(s)):
  if s[i-1] <= s[i]:
    alph_substr = alph_substr + s[i]
  else:
    # we have to start over, since we're not alphabetical anymore
    print(alph_substr)
    alph_substr = s[i]
print(alph_substr)

Upvotes: 1

Related Questions