如何循环结束并跳过循环?

时间:2012-07-08 07:07:32

标签: python loops

如果用户输入'no',我该怎么做才能使程序无法通过for循环。如果用户输入'no',我不希望它为tmpfile.write(line)。

def remove():
    coname = raw_input('What company do you want to remove? ') # company name
    f = open('codilist.txt')
    tmpfile = open('codilist.tmp', 'w')
    for line in f:
        if coname.upper() in line:
            while True:
                answer = raw_input('Are you sure you want to remove ' + line.upper() + '?')
                if answer == 'yes':
                    print line.upper() + '...has been removed.'               
                elif answer == 'no':
                    break  # HERE IS WHERE I NEED HELP
                else:
                    print 'Please choose yes or no.'                   
        else:
            tmpfile.write(line)
    else:
        print 'Company name is not listed.'
    f.close()
    tmpfile.close()
    os.rename('codilist.tmp', 'codilist.txt')

5 个答案:

答案 0 :(得分:3)

设置一个标志变量,然后跳出while循环。然后在for循环中,检查标志是否已设置,然后中断。

PS:如果一个循环

答案 1 :(得分:2)

最简单的方法是创建一个获取用户输入的函数:

def get_yes_or_no(message):
    while True:
        user_in = raw_input(message).lower()
        if user_in in ("yes", "no"):
            return user_in

并修改原来的功能:

def remove():
    coname = raw_input('What company do you want to remove? ') # company name
    f = open('codilist.txt')
    tmpfile = open('codilist.tmp', 'w')
    for line in f:
        if coname.upper() in line:
            answer = get_yes_or_no('Are you sure you want to remove ' + line.upper() + '?')
            #answer logic goes here              
        else:
            tmpfile.write(line)
    else:
        print 'Company name is not listed.'
    f.close()
    tmpfile.close()
    os.rename('codilist.tmp', 'codilist.txt')

答案 2 :(得分:1)

Python有例外,您可以使用它来代替GOTO类型的构造。

class Breakout(Exception):
    pass

def remove():
    coname = raw_input('What company do you want to remove? ') # company name
    f = open('codilist.txt')
    tmpfile = open('codilist.tmp', 'w')
    try:
        for line in f:
            if coname.upper() in line:
                while True:
                    answer = raw_input('Are you sure you want to remove ' + line.upper() + '?')
                    if answer == 'yes':
                        print line.upper() + '...has been removed.'
                    elif answer == 'no':
                        raise Breakout()
                    else:
                        print 'Please choose yes or no.'
            else:
                tmpfile.write(line)
        else:
            print 'Company name is not listed.'
    except Breakout:
        pass

    f.close()
    tmpfile.close()
    os.rename('codilist.tmp', 'codilist.txt')

注意在中间出现异常的地方。

答案 3 :(得分:0)

你必须将整个for循环放在一个函数中并使用return来摆脱它:

def find_and_remove(f,coname,tmpfile):
    for line in f:
        if coname.upper() in line:
            while True:
                answer = raw_input('Are you sure you want to remove ' + line.upper() + '?')
                if answer == 'yes':
                    print line.upper() + '...has been removed.'               
                elif answer == 'no':
                    return  # HERE IS WHERE I NEED HELP
                else:
                    print 'Please choose yes or no.'                   
        else:
            tmpfile.write(line)
    else:
        print 'Company name is not listed.'

def remove():
    coname = raw_input('What company do you want to remove? ') # company name
    f = open('codilist.txt')
    tmpfile = open('codilist.tmp', 'w')
    find_and_remove(f,coname,tmpfile)
    f.close()
    tmpfile.close()
    os.rename('codilist.tmp', 'codilist.txt')

答案 4 :(得分:0)

当您跳过一行时,不使用无限循环和break,而是使用循环条件中的标志来区分三种情况(跳过,删除和无效答案)。您设置标志以在跳过情况下退出循环,在删除情况下中断,并将标志保持原样在无效的答案情况中。这允许您使用else的{​​{1}}子句(如果while退出,则会因为条件变为false而触发)以检测跳过案例。从那里开始,您可以使用while跳转到for循环的下一次迭代(或使用continue跳过所有其余行 - 从问题中不太清楚你想要的,但不同的是关键字的变化):

break