#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11

    Need to restart program after last command


    I am trying to make a calculator that after the last operation is done, the program restarts.
    This is the ending code:
    Code:
     amount = input("Enter The Amount Saved Per Period: ")
                period = input("Enter The Amount of Periods: ")
                print "Amount Saved: ", amount * period
                print "Thank You For Using OS Calc", version
    I have look in the forums and have not found the answer to this.
    Thanks,
    Nick
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Wrap your code inside a while True loop...
    Allow them to exit the program by inputing 'quit' or 'q'

    example:

    Code:
    while True: 
        answer = raw_input('Please choose a function or type "quit" to exit:')
    
        if answer == 'your first function here': 
            print 'do your first function'
    
        elif answer == 'your second function here': 
            print 'do your second function'
    
        elif answer == 'quit': 
            break
    Code Block Generated With Py2Html
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    Wrap your code inside a while True loop...
    Allow them to exit the program by inputing 'quit' or 'q'

    example:
    Code:
    while True: 
        answer = raw_input('Please choose a function or type "quit" to exit:')
    
        if answer == 'your first function here': 
            print 'do your first function'
    
        elif answer == 'your second function here': 
            print 'do your second function'
    
        elif answer == 'quit': 
            break
    Code Block Generated With Py2Html
    I tried this and what I got is that the menu that shows up is repeated when a option is entered.
    Thanks,
    Nick
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Hehe,yes,probably because you weren't inputing a defined answer...In the example,you need to type in "your first function here" or "your second function here" or "quit" to get a response(I didn't know what words you used for your functions,or how many functions you have etc.Perhaps posting more of your code would avoid this in the future).Otherwise it just loops thru until you type in a response it reconizes.Perhaps I should have worded it better and included the "else" statement that I assumed you would use...That was very general example,but maybe one thats a little more specific was called for:

    Code:
    thankyou = "Thank You For Using OS Calc, Version X \n"
    
    while True: 
        print 'You can calculate "Total" or "Average" Savings, or "Quit"'
        choice = raw_input('What would you like to calculate? ')
    
        if choice == 'Total': 
            amount = input("\nEnter The Amount Saved Per Period: ")
            period = input("Enter The Amount of Periods: ")
            print "Amount Saved: ", amount*period
            print thankyou
    
        elif choice == 'Average': 
            amount = input("\nEnter The Total Amount Saved: ")
            period = input("Enter The Amount of Periods: ")
            print "Average Amount Saved Per Period: ", amount/period
            print thankyou
    
        elif choice == 'Quit': 
            print '\n'+thankyou+'Good Bye!'
            break
    
        else: 
            print '\nInvalid input!\n'
    Code Block Generated With Py2Html

    p.s. Incase you're wondering, whenever I put a '\n' in there,it's just for spacing to make things easier on the eye.It's the equivalent of typing and hitting <enter> to skip a line.
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

IMN logo majestic logo threadwatch logo seochat tools logo