【问题标题】:I cannot find my error and it repeats forever我找不到我的错误,它永远重复
【发布时间】:2021-12-26 16:47:15
【问题描述】:

下面的while循环意味着永远重复(因此i=0从未在循环内修改)。循环有一个checkpoint,所以每一个读取行都会从它停止的地方继续。

但是检查点不起作用,有什么办法可以解决这个问题吗?好的,除非完成,否则我不会修改 i var(这是一个很大的建议),因此它会重复,恢复检查点,并写入数据,重复。但是我想说的是检查点被破坏了,程序在读取完所有文件后不会结束。

代码

i = 0
while i == 0:
  file1 = open('APPMAKE', 'r')
  Lines = file1.readlines()
 
  count = 0
  checkpoint = 0
  ccount = 1
  modeset = 0
  # Strips the newline character
  for line in Lines:
      count += 1
      ccount += 1
      modeset += 1
      if ccount > checkpoint:
        checkpoint += 1
        if modeset > 2:
           modeset == 1
        if modeset == 1:
           elfexecutable = line
        if modeset == 2:
           appname = line
           checkpoint = count + 1
           break
  #writing data
  file1.close()
  file2 = open(appname + ".app", "a")  # append mode
  file2.write(elfexecutable + "\n")
  file2.close()
    
    

我把这个放在APPMAKE文件中测试:

elf.elf
ap.app
elf.elf
ap.app
elf.elf
ap.app
elf.elf
ap.app

哦,我制作了一个手册来使用这个 Python 脚本。这可能帮助你理解它:

APP MAKE INSTRUCTIONS
=====================

First, to generate app, create a APPMAKE file. Next, add repeated code scripts
that are like this:

[elf file to link to]
[app file name]
...

Then, you made a APPMAKE file! To make more configs, list more of the scripts above.
Remember yo replace the things in the []s! Also, you can make a app run more than
1 elf you can make more than 1 configuration for each file. Now, how do you
run a APPMAKE file? Well first go copy & paste (do this sentence if your project is
not a template) the elfexepreferences.sh file into the folder with the
APPMAKE file. (do this even if your project is a template -->) Now, edit the file
you just pasted and edit the part that looks like this:

elfexecute () {
   [function to execute elf] $1
}

Edit the []s depending on what system your apps are running on. (the elf
exepreferences file needs to be in every dir with a APPMAKE file.) And, getting close
to finished, copy and paste the elfexecuter.sh to your programs's main dir if it is
not already there. Then, in the same dir as the elfexecuter.sh file, add a
appexecute file and add a list of the APP files. Now, wanna make an app? To
execute the APPMAKE file, copy and paste appmake.py to the same dir
as the programs if it is not already there. Execute it and your app is
compiled. NOTICE: appexecute conatins names of .app files! Add the extensions!

So, I obeyed with my manual and tested the first few steps and got this to repeat and write a ENDLESS amount of elf.elf to my app file! Can someone please help!

我的新编辑,但它仍然不起作用:

checkpoint = 0
i = 5
while i == 5:
  file1 = open('APPMAKE', 'r')
  Lines = file1.readlines()
 
  count = 0
  ccount = 0
  modeset = 0
  # Strips the newline character
  for line in Lines:
      count += 1
      ccount += 1
      modeset += 1
      if modeset > 2:
           modeset == 1
      if ccount > checkpoint:
        checkpoint += 1
        if modeset == 1:
           elfexecutable = line
        if modeset == 2:
           appname = line
           checkpoint = count + 1
           break
  #writing data
  file1.close()
  file2 = open(appname + ".app", "a")  # append mode
  file2.write(elfexecutable + "\n")
  file2.close()
    
    

SOO,以上是我最新的编辑,但它仍然无法正常工作!任何帮助表示赞赏。我听取了新编辑的所有建议。

【问题讨论】:

  • 为什么有i = 0 和while 循环?好像没有办法退出。
  • 哦,等等,它意味着永远重复,它有一个“检查点”,所以每一个读取的行都会从它停止的地方继续。有什么办法可以解决吗?抱歉,不太清楚。
  • 等等,所以你想要它永远重复吗?那有什么问题呢?请描述您的输出与您想要的有何不同。
  • 检查点在每个循环开始时不断重置为 0,为什么不一次解析所有字符串并每两行处理一次呢? if modeset > 2 中的 modeset == 1 也不会将其设置回 1。
  • 好的,谢谢大家的帮助!!但是,我仍然有这个问题,我把代码换成了这个:(看帖子。)

标签: python infinite-loop


【解决方案1】:

您需要在while i==0: 循环内将i 设置为非零值,否则它将无限期地重复整个过程。

我怀疑您想在处理完所有行后退出主循环。当检查点之外没有行时会发生这种情况(您只需要在主循环之外进行初始化)。

当 for 循环没有被打破时,你可以通过在 for 循环中添加 else: 语句来打破 while 循环

for line in line:
    ...
    if ccount > checkpoint:
       ...
       if modeset == 2:
          appname = line
          checkpoint = count + 1
          break
else: break      # will break the while if the for-loop wasn't broken
                 # notice that this else: is at the same level as the for-loop

【讨论】:

  • 好的,感谢您的帮助!
  • 哦,等等,它意味着永远重复,它有一个“检查点”,所以每一个读取的行都会从它停止的地方继续。有什么办法可以解决吗?很抱歉不太清楚。
  • @bigDoge 这个澄清属于你的问题。我编辑了您的问题并将其放在顶部。
【解决方案2】:

问题是您没有该循环的退出条件,并且该变量没有再次使用。

如果您删除i = 0,它应该退出,您在它正下方的while循环,并将所有内容取消一级。或者您需要创建退出循环的条件,例如在某处设置i = 1。中断也应该退出内部循环,但它会在开始处再次重复,这可能不是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2013-09-19
    • 2017-05-22
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多