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

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Run script again?


    I'm looking for a way to re-run the script after finishing. I want to do something like this.

    PHP Code:
    # ------------

    # Script

    # -----------

    print "Finished."
    var = 0
    while(var != "Yes" and var != "yes" and var != "no" and var !="No"):
        var = 
    raw_input("Do you want to re-run the script?": )
        if var == 
    "Yes" or "yes":
            
    # Execute again the script
        
    elif var == "No" or var == "no":
            break 
    # Finish script
        
    else:
            print 
    "Try again" 
    But I do not know if I should define a function or is there any module related to that. If any can help me, i'll be grateful!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Use many little functions. This solution is good.
    Code:
    def your_prompt(message='? ', responses='yes no'.split()):
        answers = set(word.lower() for word in responses)
        while True:
            answer = raw_input(message)
            answer = answer.lower()  # you'll have less comparisons with a common case.
            if answer in answers:
                return answer
            print("Try again")
    
    def script():
        print('important computations take place...')
        print('Whew!!!  My gpu is sore.')
    
    def main():
        AFFIRM = 'yes'
        answer = AFFIRM
        while answer == AFFIRM:
            script()
            answer = your_prompt("Do you want to re-run the script? ",[AFFIRM,'no'])
    
    if '__main__' == __name__:
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    This solution is BAD, because it recurses for no reason. I you answer yes enough times you'll get a stack error. Your code looks as if you had this in mind.
    Code:
    # your_prompt from previous post
    
    AFFIRM = 'yes'.lower()
    
    def script():
        print('important computations take place...')
        print('Whew!!!  My gpu is sore.')
        if your_prompt("Do you want to re-run the script? ",[AFFIRM,'no']) == AFFIRM:
            script()   ##### recursion to unknown depth.  ###########
    
    def main():
        script()
    
    if '__main__' == __name__:
        main()
    Last edited by b49P23TIvg; March 9th, 2013 at 07:54 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    This solution is BAD, because it recurses for no reason. I you answer yes enough times you'll get a stack error. Your code looks as if you had this in mind.
    Code:
    # your_prompt from previous post
    
    AFFIRM = 'yes'.lower()
    
    def script():
        print('important computations take place...')
        print('Whew!!!  My gpu is sore.')
        if your_prompt("Do you want to re-run the script? ",[AFFIRM,'no']) == AFFIRM:
            script()   ##### recursion to unknown depth.  ###########
    
    def main():
        script()
    
    if '__main__' == __name__:
        main()
    Thanks for replying (as always). But when I enter "yes", the loop does not end and asks again what I do.

IMN logo majestic logo threadwatch logo seochat tools logo