繁体   English   中英

第一次让for循环忽略某些东西

[英]Making for loop ignore something first time through

我正在为CS类进行加密分配,并在其中使用逻辑映射部分地通过ord()和chr()更改字符。

对于我的代码,我正在使用for循环,并且在使用特定命令(logMap)之前我需要使其运行一次

from logistic import logMap
from genAmpSeed import genAmpSeed
from cipherChr import cipherChr

password=input("Enter password: ")
amp,seed=genAmpSeed(password)
line=input("Enter line to cipher: ")

def logMap(a,x):
    ans=(a*x)*(1-x)
    return ans

def line2cipher(amp,seed,line):
    acc=''
    for i in line:
        res=logMap(amp,seed)
        offset=int(96*res)
        acc+= cipherChr(i,offset)
    print(acc,res)

所以我需要第一次运行它,但是第一次使用genAmpSeed( genAmpSeed.py )的默认值,然后从那时起使用logMap的默认值

您可以使用slice忽略字符串的第一个字符。

def line2cipher(amp,seed,line):
    acc=''
    for i in line[1:]:
        res=logMap(amp,seed)
        offset=int(96*res)
        acc+= cipherChr(i,offset)
    print(acc,res)

编辑:

如果您想要更通用的东西,可以使用一个标志来检查它是否是第一次运行循环:

first = True
for i in range(10):
    if first:
        first = False
        # Do whatever you need to do for the first time
        continue
    # Do something...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM