|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今天尝试用python实现曲路密码,原理如下图,很简单
然后我的函数是这样写的
- def jiami(str1,row,column):
- while str1.find(' ')!=-1 : #这里的while循环是把明文里的空格去掉
- a=str1.find(' ')
- str1=str1[:a]+str1[a+1:]
- if len(str1)/column == int(len(str1)/column) and len(str1)/column<=row : #下面4个if修正行数
- row=len(str1)/column
- if len(str1)/column!=int(len(str1)/column) and int(len(str1)/column)+1<=row :
- row=int(len(str1)/column)+1
- if (len(str1)/column == int(len(str1)/column) and len(str1)/column>row) or (len(str1)/column!=int(len(str1)/column) and int(len(str1)/column)+1>row) :
- print('行数太少,无法容纳全部文字!')
- return 0
- mi=[]
- for i in range(row):
- mi.append(str1[:column])
- str1=str1[column:]
- count=0
- str2=''
- for i in range(column-1,-1,-1) :
- if count%2==0 :
- for j in range(row-1,-1,-1):
- if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
- continue
- str2=str2+mi[j][i]
- count+=1
- else :
- for j in range(row):
- if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
- continue
- str2=str2+mi[j][i]
- count+=1
- return str2
复制代码
但是一调用就会出错,如图
我尝试拆开这个函数一步一步来,先定义str1=‘abcdefghi’
然后定义mi=[],row=column=3
然后输入代码
- for i in range(row):
- mi.append(str1[:column])
- str1=str1[column:]
复制代码
这时再看mi,已经变成了
然后定义str2=‘’(空字符串),count=0
继续输入
- for i in range(column-1,-1,-1) :
- if count%2==0 :
- for j in range(row-1,-1,-1):
- if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
- continue
- str2=str2+mi[j][i]
- count+=1
- else :
- for j in range(row):
- if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
- continue
- str2=str2+mi[j][i]
- count+=1
复制代码
这时再看str2,结果正确
可是为什么像一开始那样直接调用我的函数就会报错呢?
你最开始mi=[]空列表,想用mi[j][i]这元素当然不可能
|
|