鱼C论坛

 找回密码
 立即注册
查看: 2962|回复: 3

请各位大神看看我自己写的这个程序错在那,这是零基础python第19课第二道编程题

[复制链接]
发表于 2017-5-3 18:01:10 | 显示全部楼层 |阅读模式

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

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

x
def count():
    length=len(str1)
    str2='QAZWSXEDCRFVTGBYHNUJMIKOLP'
    for i in range(length):
        if i>=4:
            m=i-4
            s=i+4
            while i!=m:
                if str1[i-1] in str2:
                    i=i-1
                else:
                    break
                while i!=s:
                     if str1[i+1] in str2:
                         i=i+1
                     else:
                        break
            if str1[m] not in str2:
                if str1[s] not in str2:
                    print(str1[i],end='')
            else:
                break
        else:
            continue
count()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-5-3 18:20:42 | 显示全部楼层
没看到str1的值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-3 18:42:39 | 显示全部楼层

...str1加上也是运行不出来  这不是主要问题  我在打的时候忘打了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-3 19:38:17 | 显示全部楼层
  1. str1='AAAAAAAaa'
  2. #你的代码主要有两个问题
  3. def count():
  4.     length=len(str1)
  5.     str2='QAZWSXEDCRFVTGBYHNUJMIKOLP'
  6.     for i in range(length):
  7.         if i>=4:
  8.             print i
  9.             m=i-4
  10.             s=i+4#问题1:当你的str1长度大于4小于9时,str1[i+1]会超出str1可取值的范围

  11.             #问题2由此开始,假设str1的长度大于等于9
  12.             while i!=m:
  13.                 if str1[i-1] in str2:
  14.                     i=i-1#你这里让i减小
  15.                     print i#添加一个print
  16.                 else:
  17.                     break
  18.                 while i!=s:#这个循环在上个循环的缩进之内,所以上个循环每一次执行,这个循环也会执行
  19.                      if str1[i+1] in str2:
  20.                          i=i+1#你这里又让增大,就算上面把i减到m,这里也会让i加回去
  21.                          print i#添加一个print
  22.                      else:
  23.                         break
  24.             if str1[m] not in str2:
  25.                 if str1[s] not in str2:
  26.                     print str1[i],
  27.             else:
  28.                 break
  29.         else:
  30.             continue
  31. count()
复制代码

总的来说你的代码卡死在两个while循环里了,添加我跟你写的两个print,你就能看到你的代码运行的时候一直在运算,只是出不来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 08:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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