2 Replies - 547 Views - Last Post: 06 December 2012 - 10:16 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

How to keep the program running?

Posted 06 December 2012 - 07:39 AM

I have a code, and I am trying to figure out how to keep it running until the user exist it, here is a sample example:

question = input("Would you like quit the program?\nY. Yes\tN. No")

while(True):
    if question=="y":
        print("Keep the program running.")
        break

    elif question=="n":
        print("Program is closing.")
        break



So my question is, how would you keep the .exe program running? I tried to search it up and most of them involves in Python 2.6, that they simply but raw_input in the end of program and asking the user to press the program to shut down.

My second question related to first is:
What if the user wants to run the program multiple times, how would I do it? I was wondering that the user would need a nested loop to run the entire program. The first while will be keep in track of the entire program, meanwhile the second while loop will keep in track of the options that it given.

while(True):
    while(True):



So please any suggestions on how to keep the .exe program running?

Is This A Good Question/Topic? 0
  • +

Replies To: How to keep the program running?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,579
  • Joined: 19-March 11

Re: How to keep the program running?

Posted 06 December 2012 - 08:01 AM

Simple way:

def main_loop():
  # program here

def run():
  keep_going = true
  while keep_going:
    main_loop()
  # query user and set keep_going accordingly



Was This Post Helpful? 1
  • +
  • -

#3 npanagakis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-December 12

Re: How to keep the program running?

Posted 06 December 2012 - 10:16 PM

Another way is this:
while(True):
    question = raw_input("Would you like quit the program?\nY. Yes\tN. No")
    if question=="n":
        print("Keep the program running.")
    elif question=="y":
        print("Program is closing.")
        break


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1