0

everybody! I'm very new to Python and I'm trying to learn some essentials. I bumped with a task while passing online courses for beginners. The task was to create a programm, which would read a string of some letters, for example, 'aaabbbccc', and output 'a3b3c3' (another example: it could be 'abccaab', then programm should output 'a1b1c2a2b1'). I thought I found a way to deal with it using while loop, but at the end, when I add elif-construction in if else, it doesn't work: when i input my string and press enter, it just passes to the next line and seems like programm haven't started at all. I'll be very thankfull for any comment or advise about how to fix it or any alternative ideas that migth help to solve the task.

n = input()
i = 0
j = i + 1
s = 1
m = ''
while j < len(n):
    if j == len(n) - 1:
        s += 1
        m += n[j] + str(s)
    elif n[i] != n[j]:
        m += n[i] + str(s)
        i += 1
        j += 1
        s = 1
    else:
        i += 1
        j += 1
        s += 1
print(m)
1
  • 2
    In caseif j == len(n) - 1 nothing happens to j; you get a never ending loop. Add after m += n[j] + str(s) inside that case print(j) to see for yourself. Commented Apr 2, 2020 at 23:15

2 Answers 2

2

You need to always increment j or else you will get an infinite loop

while j < len(n):
    if j == len(n) - 1:
        s += 1
        m += n[j] + str(s)
    elif n[i] != n[j]:
        m += n[i] + str(s)
        i += 1
        s = 1
    else:
        i += 1
        s += 1
    j += 1

You can avoid this kind of error by using for loops (by nature they are less prone to infinite behavior) e.g.:

s = 1
m = ''
for i in range(len(n) - 1):
    j = i + 1
    if j == len(n) - 1:
        s += 1
        m += n[j] + str(s)
    elif n[i] != n[j]:
        m += n[i] + str(s)
        s = 1
    else:
        s += 1

print(m)

p.s.: I think you have some logic issue concerning the last char

s = 1
m = ''
for i in range(len(n)):
    j = i + 1
    # handle last char
    if j >= len(n):
        if s > 1:
            # If the last char is the same as previous one
            m += n[i] + str(s)
        else:
            # if the last char is different
            m += n[i] + str(s)
    elif n[i] != n[j]:
        m += n[i] + str(s)
        s = 1
    else:
        s += 1
Sign up to request clarification or add additional context in comments.

2 Comments

do i need to mark anyhow that my problem is solved now?
@mieltn you can select the corect anwser for your question: stackoverflow.com/help/someone-answers
0

Well, the answer to your problem is "you have a infinite loop". Why?

Check this part of your code:

if j == len(n) - 1:
    s += 1
    m += n[j] + str(s)

As you can see, that block inside the if statement will execute when j == len(n)-1 meaning, the j (you are ussing to loop through n will reach the last element on the string n. So, after that line j will remain the same value and nothing else will execute, meaning the condition j < len(n) will be True forever, hence your infinite loop. Now, you have to options here:

- You can add a j+=1 inside that if block.

if j == len(n) - 1:
    s += 1
    m += n[j] + str(s)
    j += 1

- You could add j+=1 outside all if blocks and remove it from the other if blocks

while j < len(n):
    if j == len(n) - 1:
        s += 1
        m += n[j] + str(s)
    elif n[i] != n[j]:
        m += n[i] + str(s)
        i += 1
        s = 1
    else:
        i += 1
        s += 1
    j += 1

Now, there are some logic problems concerning some options. I will refactor your code and fix the problems. Also, I highly recommend you to use meaningful variable names. It will help others and you (in the future) to understand and debug your code.

word = input() 
result = "" # This will hold the result string

# Will assume user did not input an empty string. You could check that though
currentLetter = word[0] # Get the first letter
currentCount = 1 # Count it as 1
j = 1 # To start from the index 1 and iterate over word letters

while(j < len(word)):
    if word[j] != currentLetter:
        # if the letter on the current index is different from the currentLetter
        # means there was a change so we must add to the result string what
        # we counted (letter and count)
        result += currentLetter + str(currentCount)

        # Get the new letter and set its count to 1
        currentLetter = word[j]
        currentCount = 1
    else:
        # Else the letter is the same so far -> increase count
        currentCount += 1
    j += 1 # Increase index variable
else:
    # This else will execute when the while condition is false
    # add the remaining values
    result += currentLetter + str(currentCount)

print(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.