簡體   English   中英

附加 if 條件 (elif) 在 while 循環中不起作用

[英]additional if condition (elif) doesn't work in while loop

大家。 我對 Python 很陌生,我正在嘗試學習一些基本知識。 我在為初學者通過在線課程時遇到了一個任務,該任務是創建一個程序,它會讀取一些字母的字符串,例如'aaabbbccc':和output'a3b3c3'(另一個例子,它可能是'abccaab '。那么程序應該 output 'a1b1c2a2b1'),我想我找到了一種使用 while 循環處理它的方法,但最后,當我在 if else 中添加 elif 構造時:它不起作用,當我輸入我的字符串並按回車。 它只是傳遞到下一行,似乎程序還沒有開始。 對於如何修復它或任何有助於解決任務的替代想法,我將非常感謝任何評論或建議。

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)

你需要總是增加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 循環來避免這種錯誤(本質上它們不太容易出現無限行為),例如:

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)

ps:我認為您對最后一個字符有一些邏輯問題

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM