Restarting a program

Discussion in 'Python' started by Geoldr, May 22, 2008.

  1. Geoldr

    Geoldr Guest

    Hello all, I have written a simple program, and at the end of it,
    instead of it closing I would like it to restart from the beggining.
    Is there a way to do this? Put my code into a class function, or
    something?
    I guess I could do a while loop, but I think if there is a way to run
    my code if it's in a class would be an easier option. I can't seem to
    find many guides online but maybe I have not been looking in the right
    places.

    Anybody have any ideas?
     
    Geoldr, May 22, 2008
    #1
    1. Advertising

  2. On May 22, 10:59 am, Geoldr <> wrote:
    > Hello all, I have written a simple program, and at the end of it,
    > instead of it closing I would like it to restart from the beggining.
    > Is there a way to do this? Put my code into a class function, or
    > something?
    > I guess I could do a while loop, but I think if there is a way to run
    > my code if it's in a class would be an easier option. I can't seem to
    > find many guides online but maybe I have not been looking in the right
    > places.
    >
    > Anybody have any ideas?


    Putting your code in a function or class is probably the way to go.
    When I was doing C++, we'd just use a while loop for simple stuff,
    though.

    It really shouldn't be all that hard to tell the code to call up the
    beginning of the program again.

    Mike
     
    Mike Driscoll, May 22, 2008
    #2
    1. Advertising

  3. Geoldr

    Geoldr Guest

    On May 22, 10:07 am, Mike Driscoll <> wrote:
    > On May 22, 10:59 am, Geoldr <> wrote:
    >
    > > Hello all, I have written a simple program, and at the end of it,
    > > instead of it closing I would like it to restart from the beggining.
    > > Is there a way to do this? Put my code into a class function, or
    > > something?
    > > I guess I could do a while loop, but I think if there is a way to run
    > > my code if it's in a class would be an easier option. I can't seem to
    > > find many guides online but maybe I have not been looking in the right
    > > places.

    >
    > > Anybody have any ideas?

    >
    > Putting your code in a function or class is probably the way to go.
    > When I was doing C++, we'd just use a while loop for simple stuff,
    > though.
    >
    > It really shouldn't be all that hard to tell the code to call up the
    > beginning of the program again.
    >
    > Mike


    That's what I am trying to figure out, but it doesn't seem to work. Do
    you have any example code of classes/functions that work for you?
     
    Geoldr, May 22, 2008
    #3
  4. On May 22, 1:38 pm, Geoldr <> wrote:
    > On May 22, 10:07 am, Mike Driscoll <> wrote:
    >
    >
    >
    > > On May 22, 10:59 am, Geoldr <> wrote:

    >
    > > > Hello all, I have written a simple program, and at the end of it,
    > > > instead of it closing I would like it to restart from the beggining.
    > > > Is there a way to do this? Put my code into a class function, or
    > > > something?
    > > > I guess I could do a while loop, but I think if there is a way to run
    > > > my code if it's in a class would be an easier option. I can't seem to
    > > > find many guides online but maybe I have not been looking in the right
    > > > places.

    >
    > > > Anybody have any ideas?

    >
    > > Putting your code in a function or class is probably the way to go.
    > > When I was doing C++, we'd just use a while loop for simple stuff,
    > > though.

    >
    > > It really shouldn't be all that hard to tell the code to call up the
    > > beginning of the program again.

    >
    > > Mike

    >
    > That's what I am trying to figure out, but it doesn't seem to work. Do
    > you have any example code of classes/functions that work for you?


    No...but I through some concept code together that does the basics:

    <code>

    def repeater():

    for i in range(10):
    print i

    def main():
    ret = 'Y'
    while 1:
    if ret.upper() == 'Y':
    repeater()
    else:
    print 'Program finished...goodbye!'
    break
    ret = raw_input('Do you want to continue? (Y/N)')

    if __name__ == '__main__':
    main()

    </code>

    I found that using the while was the easiest to create on short
    notice. You could probably do it with recursion too, but I'm not
    especially good at that.

    Another idea is to have some kind of sentinel value that both
    functions can access and use it somehow to tell whether or not to
    repeat.

    Hope that helps you get going.

    Mike
     
    Mike Driscoll, May 22, 2008
    #4
  5. Geoldr

    Geoldr Guest

    On May 22, 11:58 am, Mike Driscoll <> wrote:
    > On May 22, 1:38 pm, Geoldr <> wrote:
    >
    >
    >
    > > On May 22, 10:07 am, Mike Driscoll <> wrote:

    >
    > > > On May 22, 10:59 am, Geoldr <> wrote:

    >
    > > > > Hello all, I have written a simple program, and at the end of it,
    > > > > instead of it closing I would like it to restart from the beggining.
    > > > > Is there a way to do this? Put my code into a class function, or
    > > > > something?
    > > > > I guess I could do a while loop, but I think if there is a way to run
    > > > > my code if it's in a class would be an easier option. I can't seem to
    > > > > find many guides online but maybe I have not been looking in the right
    > > > > places.

    >
    > > > > Anybody have any ideas?

    >
    > > > Putting your code in a function or class is probably the way to go.
    > > > When I was doing C++, we'd just use a while loop for simple stuff,
    > > > though.

    >
    > > > It really shouldn't be all that hard to tell the code to call up the
    > > > beginning of the program again.

    >
    > > > Mike

    >
    > > That's what I am trying to figure out, but it doesn't seem to work. Do
    > > you have any example code of classes/functions that work for you?

    >
    > No...but I through some concept code together that does the basics:
    >
    > <code>
    >
    > def repeater():
    >
    >     for i in range(10):
    >         print i
    >
    > def main():
    >     ret = 'Y'
    >     while 1:
    >         if ret.upper() == 'Y':
    >             repeater()
    >         else:
    >             print 'Program finished...goodbye!'
    >             break
    >         ret = raw_input('Do you want to continue? (Y/N)')
    >
    > if __name__ == '__main__':
    >     main()
    >
    > </code>
    >
    > I found that using the while was the easiest to create on short
    > notice. You could probably do it with recursion too, but I'm not
    > especially good at that.
    >
    > Another idea is to have some kind of sentinel value that both
    > functions can access and use it somehow to tell whether or not to
    > repeat.
    >
    > Hope that helps you get going.
    >
    > Mike


    Thank you, the "def" option works the best.
     
    Geoldr, May 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LJ
    Replies:
    0
    Views:
    1,127
  2. Mark Feather

    Problems after restarting IIS ????

    Mark Feather, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    459
    Mark Feather
    Jul 14, 2003
  3. kevin donne

    Restarting my program

    kevin donne, Feb 1, 2011, in forum: C Programming
    Replies:
    14
    Views:
    550
    Turtle Wizard
    Feb 1, 2012
  4. pgodfrin

    Restarting a Program mid stream

    pgodfrin, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    97
    pgodfrin
    Nov 25, 2008
  5. Peace
    Replies:
    3
    Views:
    91
    dieter
    Mar 22, 2014
Loading...

Share This Page