如何从里面开始循环,条件结果在里面,跳过其他部分

时间:2016-04-06 14:33:53

标签: python-3.x

我处于循环中,而True则使用不同的代码。如果我有" x"在文本文件的行中,如何跳过PART 2并从头开始循环,但如果不是" x"我想通过第2部分

在这种情况下如何做到这一点,您能否准确地告诉我这个代码:

import time

loop = 1

while True:

    try:

        print ('PART 1')

        text = open('D:\my_path\text.txt', "r")
        searchlines = text.readlines()
        for z, line in enumerate(searchlines):
            if ('y') in line:
                for l in searchlines:
                    print (z, line,' if "y" go to top, skip PART 2')
                    time.sleep(1)
                    break
            if not ('y') in line: #not sure about "if not" if needed here
                print (z, line,'not "y" pass PART 2')
                time.sleep(1)
                break
        continue
    except ValueError:
        print ("Could not convert data to an integer.")


    try:

        print ('PART 2')

    except ValueError:
        print ("Could not convert data to an integer.")

    loop += 1
    time.sleep(1)

看起来像结果" y"但不,只是继续循环:

PART 1
0 y  if "y" go to top, skip PART 2'
PART 1
0 y  if "y" go to top, skip PART 2'

糟糕的结果,现在没有y:

PART 1
0 k if not "y" pass PART 2
PART 1
0 k if not "y" pass PART 2

其他方式继续内部,但同样错误的结果" y"在文本文件中:

PART 1
0 y  if "y" go to top, skip PART 2
PART 2

没有" y"在文本文件中:

PART 1
PART 2

2 个答案:

答案 0 :(得分:0)

if ('y') in line:
    for l in searchlines:
        print (z, line,' if "y" go to top, skip PART 2')
        time.sleep(1)
        break
    continue  # <- add here
if not ('y') in line: #not sure about "if not" if needed here
    print (z, line,'not "y" pass PART 2')
    time.sleep(1)
    break
# continue -> delete this

虽然我怀疑这是否是你正在做的最好的方式。

答案 1 :(得分:0)

我对此进行了重大的重写,利用函数将第1部分和第2部分分开。这样你可以利用嵌套来确定你是否应该完成第2部分。它还可以使您的部件代码分开。

import time

loop = 1
text = open('text.txt', "r")
searchlines = text.readlines()  # Pull the file out of the loop, only load once.

def part_1(): # Keep part 1 & 2 in functions, easier to maintain.
    try:
        print ('PART 1')
        for z, line in enumerate(searchlines):
            got_y = 'y' in line
            if got_y:
                print (z, line,' if "y" go to top, skip PART 2')
                # Do whatever you need to do if there is a y in part 1
            else:
                print (z, line,'not "y" execute PART 2')
                # Do whatever you need to do if there is NO y in part 1
            time.sleep(1)
            yield not got_y, line  # Generator function, so Part 2 will be considered after each line
    except ValueError:
        print ("Could not convert data to an integer.")

def part_2(line):
    try:
        print ('PART 2')
        # Part 2 code goes here
        print(line)
    except ValueError:
        print ("Could not convert data to an integer.")

while True:      
    for needs_part_2, line in part_1():  # Iterate through generator (1 item at a time)
        if needs_part_2:  # Boolean describes if we need to do part 2 or not.
            part_2(line)  # Needs to be nested, but function seperates code.

    loop += 1
    time.sleep(1)