0

How do I make it where if the user enters 'no' the program won't go through the for loop either. I don't want it to tmpfile.write(line) if the user enters 'no'.

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')
1
  • That's one case when GOTO is legal.
    – user529758
    Jul 8, 2012 at 7:11

5 Answers 5

3

Set a flag variable and then break out of the while loop. Then in the for loop, check if the flag is set, and then break.

PS: if is not a loop

2
  • 2
    The final else is attached to the for - it will trigger if the loop finishes without a break.
    – lvc
    Jul 8, 2012 at 7:27
  • @vaisakh: No problem. Edited so I could reverse my downvote (it was locked in) Jul 8, 2012 at 7:39
2

The easiest way to do this is to create a function that gets user input:

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

And modify your original function like so:

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')
0
1

Python has exceptions, which you can use in place of a GOTO type of construction.

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')

Notice where in exception is raised in the middle there.

0

You have to put the whole for loop in a function and use return to get out of it:

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')
0

Instead of using an infinite loop and breaking when you skip a line, differentiate between the three cases (skip, remove, and invalid answer) using a flag in the loop condition. You set the flag to exit the loop in the skip case, break in the remove case, and leave the flag as-is in the invalid answer case. This lets you use the else clause of the while (which is triggered if the while exits because the condition became false) to detect the skip case. From there, you can jump ahead to the next iteration of the for loop using continue (or skip all the rest of the lines using break - it isn't quite clear from the question which you intend, but the difference is change of keyword):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.