鱼C论坛

 找回密码
 立即注册
查看: 1366|回复: 2

[已解决]这个函数拆开在idle中实现没问题,但是放在一起,调用就错误

[复制链接]
发表于 2018-1-24 11:07:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天尝试用python实现曲路密码,原理如下图,很简单 曲路密码.PNG
然后我的函数是这样写的
  1. def jiami(str1,row,column):
  2.     while str1.find(' ')!=-1 :   #这里的while循环是把明文里的空格去掉
  3.         a=str1.find(' ')
  4.         str1=str1[:a]+str1[a+1:]
  5.     if len(str1)/column == int(len(str1)/column) and len(str1)/column<=row :    #下面4个if修正行数
  6.         row=len(str1)/column
  7.     if len(str1)/column!=int(len(str1)/column) and int(len(str1)/column)+1<=row :
  8.         row=int(len(str1)/column)+1
  9.     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) :
  10.         print('行数太少,无法容纳全部文字!')
  11.         return 0
  12.     mi=[]
  13.     for i in range(row):
  14.         mi.append(str1[:column])
  15.         str1=str1[column:]
  16.     count=0
  17.     str2=''
  18.     for i in range(column-1,-1,-1) :
  19.         if count%2==0 :
  20.             for j in range(row-1,-1,-1):
  21.                 if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
  22.                     continue
  23.                 str2=str2+mi[j][i]
  24.             count+=1
  25.         else :
  26.             for j in range(row):
  27.                 if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
  28.                     continue
  29.                 str2=str2+mi[j][i]
  30.             count+=1
  31.     return str2
复制代码

但是一调用就会出错,如图
调用出错.PNG
我尝试拆开这个函数一步一步来,先定义str1=‘abcdefghi’
然后定义mi=[],row=column=3
然后输入代码
  1. for i in range(row):
  2.         mi.append(str1[:column])
  3.         str1=str1[column:]
复制代码

这时再看mi,已经变成了
mi.PNG
然后定义str2=‘’(空字符串),count=0
继续输入
  1. for i in range(column-1,-1,-1) :
  2.             if count%2==0 :
  3.                 for j in range(row-1,-1,-1):
  4.                     if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
  5.                         continue
  6.                     str2=str2+mi[j][i]
  7.                 count+=1
  8.             else :
  9.                 for j in range(row):
  10.                     if len(str1)/column!=int(len(str1)/column) and i>=(len(str1)%column) and j==row-1:
  11.                         continue
  12.                     str2=str2+mi[j][i]
  13.                 count+=1
复制代码

这时再看str2,结果正确 结果.PNG
可是为什么像一开始那样直接调用我的函数就会报错呢?
最佳答案
2018-1-24 11:18:50
你最开始mi=[]空列表,想用mi[j][i]这元素当然不可能
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-24 11:18:50 | 显示全部楼层    本楼为最佳答案   
你最开始mi=[]空列表,想用mi[j][i]这元素当然不可能
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-24 14:07:56 | 显示全部楼层
塔利班 发表于 2018-1-24 11:18
你最开始mi=[]空列表,想用mi[j]这元素当然不可能

我后来用mi.append()方法给mi里面增加元素了啊,每个元素都是字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-6 06:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表