Home Resetting ALL variables when running main() in a loop
Reply: 1

Resetting ALL variables when running main() in a loop

user9080591
1#
user9080591 Published in 2018-01-09 17:34:18Z

I'm looking to run a program again when asking the user: "Do you want to run the program again". No going to exit, and yes looping around and running main again. However I've noticed that python keeps the old variable assignments stored. Is there a quick way to clear the memory so that when the program loops again stores new variables?

At the moment i've just got: Please close the program and reopen it! Want to make it more elegant:

Here's what i've got so far:

def main()
    print("Do you want to solve another problem?")
    answer = input()
    while answer not in["yes","no"]:
        answer = input()

    if answer == "yes":
        print("Please close the program and rerun it")
        #main() #<-this is not working as expected
    else:
        exit()
Mike Tung
2#
Mike Tung Reply to 2018-01-09 17:37:35Z

One thing you can do is localize the variables and have clean up function to handle starting a new session.

def main()
    print("Do you want to solve another problem?")
    answer = input()
    while answer not in["yes","no"]:
        answer = input()

    if answer == "yes":
        answer = None
        print("Please close the program and rerun it")
        #main() #<-this is not working as expected
    else:
        exit()

The difference is if answer == 'yes' will set answer = None which clears it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301203 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO