Well, I have recently created a "translation" program. It basically asks what languages you would like to translate between, then goes to the appropriate page on a translation website. I have integrated an option to keep using a language, rather than having to start at the main menu again. Except, my loop doesn't seem to want to reset. Could anybody help?

# Translate

import webbrowser

def engtospa():
    a = "http://translate.google.com/translate_t#en|es|"
    b = raw_input("What do you want to translate?\n> ")
    if b == "stop":
        loop1 = False
    else:
        webbrowser.open(a+b)
        print "Success."

def spatoeng():
    a = "http://translate.google.com/translate_t#es|en|"
    b = raw_input("What do you want to translate?\n> ")
    if b == "stop":
        loop1 = False
    else:
        webbrowser.open(a+b)
        print "Success."

# Main Menu

def main():
    print "Welcome to the translation program. Do you want to translate..."
    print "1 - English to Spanish"
    print "2 - Spanish to English"
    lang = input("> ")
    if lang == 1:
        print "Do you want to stay with this language?"
        print "1 - Yes"
        print "2- No"
        ans = input("> ")
        if ans == 1:
            loop1 = True
            print "You can exit this any time by entering the word 'stop'"
            while loop1 == True:
                engtospa()
        elif ans == 2:
            engtospa()
    elif lang == 2:
        print "Do you want to stay with this language?"
        print "1 - Yes"
        print "2- No"
        ans = input("> ")
        if ans == 1:
            loop1 = True
            print "You can exit this any time by entering the word 'stop'"
            while loop1 == True:
                spatoeng()
        elif ans == 2:
            spatoeng()

# Loop

while True:
    main()

The problem...well I will just show you a log of my script.

Welcome to the translation program. Do you want to translate...
1 - English to Spanish
2 - Spanish to English
> 1
Do you want to stay with this language?
1 - Yes
2- No
> 1
You can exit this any time by entering the word 'stop'
What do you want to translate?
> ball
Success.
What do you want to translate?
> stop
What do you want to translate?
>

It SHOULD set "loop1" to false when I enter the string "stop" because of my If statements in engtospa() and spatoeng(), but it does not for some reason. Could somebody help me out?

Thanks in advance.

The problem is that loop1 exists in two places. In the global scope and in the function scope. This means that the loop1 in the function is different to the loop1 in the main body. To get around this you can add global loop1 to your program.

This question has already been answered. Start a new discussion instead.