附加if条件(elif)在while循环中不起作用

2024-09-30 14:37:04 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好!我对Python非常陌生,我正在尝试学习一些基本知识。我在通过初学者在线课程时遇到了一项任务。任务是创建一个程序,它将读取一些字母的字符串,例如“aaabbccc”,并输出“a3b3c3”(另一个示例:它可以是“abccaab”,然后程序将输出“a1b1c2a2b1”)。我想我找到了一种使用while循环来处理它的方法,但最后,当我在if-else中添加elif构造时,它不起作用:当我输入字符串并按enter键时,它只传递到下一行,看起来程序根本没有启动。我将非常感谢任何关于如何修复它的评论或建议,或者任何可能有助于解决任务的替代想法

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)

Tags: 字符串程序lenif字母else课程elif
2条回答

您需要始终递增j,否则将得到一个无限循环

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

通过使用for循环可以避免这种错误(本质上,它们不太容易出现无限行为) 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)

附言: 我想你对最后一个字符有一些逻辑问题

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

你的问题的答案是“你有一个无限循环”。为什么?

检查代码的这一部分:

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

正如您所看到的,if语句中的块将在j == len(n)-1时执行,这意味着j(您正在使用循环通过n)将到达字符串n上的最后一个元素。因此,在该行之后j将保持相同的值,并且不会执行任何其他内容,这意味着条件j < len(n)将是True永远,因此你的无限循环。现在,你必须在这里选择:

-您可以在该if块中添加一个j+=1

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

-您可以在所有if块之外添加j+=1并将其从其他if块中移除

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

现在,一些选项存在一些逻辑问题。我将重构您的代码并修复这些问题。此外,我强烈建议您使用有意义的变量名。这将帮助其他人和您(将来)理解和调试您的代码

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)

相关问题 更多 >