为什么我的迭代不起作用


def lol(plaintext,row,col,group):
    val=''
    for ch in range(0,1000000,group):
        if ch>=row*col: #reset value of ch to the next col 
            break
            val=val+''
            ch = ch - (row+1)*group +1
            continue
        if ch>(row+2)*group: #to prevent exceedimg
            break
        val=val+(plaintext[ch])
        return val #return encrypted message
print(lol("ILIKEDRINKINGLEMONADEWITHICEIN", 6,5,5))

第一次运行 for 循环时,ch 为 0,如果 if 语句条件也不满足。

val=val+(plaintext[ch])
return val #return encrypted message

val 被设置,你的函数在设置一个字符后立即返回。 尝试取消缩进返回语句:

def lol(plaintext, row, col, group):
    val = ''
    for ch in range(0, 1000000, group):
        if ch >= row * col:  #reset value of ch to the next col
            break
            val = val + ''
            ch = ch - (row + 1) * group + 1
            continue
        if ch > (row + 2) * group:  #to prevent exceedimg
            break
        val = val + (plaintext[ch])
    return val  # this now returns after the for loop finishes.  

最新更新