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

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Help with defined function and while loop


    Here is my program so far in Python 2.7:

    def menu():
    ....print "Welcome to my bitchin' calculator!"
    ....print " "
    ....return raw_input("Would you like to add, subtract, multiply, or divide? ")
    def add(add1,add2):
    ....print add1, "+", add2, "=", add1 + add2
    def sub(sub1,sub2):
    ....print sub1, "-", sub2, "=", sub1 - sub2
    def redo(a):
    ....if a == "yes":
    ........print "OK!"
    ....else:
    ........loop = 0
    loop = 1
    choice = 0
    while loop == 1:
    ....choice = menu()
    ....if choice == "add":
    ........add(input("add this: "),input("to this: "))
    ........redo(raw_input("Would you like to start over? "))
    print "Bye bye!"

    *Note that the .... was added to indicate indention, due to this forum removing my indentions. in the actual program, the indentions are present

    I can choose to add, and then it successfully adds the numbers and starts the redo function as defined above. However when I type "no", it should set loop to 0 and stop running the while loop, but it doesn't! after I type no, it runs menu again and starts over. It never prints "Bye bye!" Can someone help a rookie out?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Code:
    def redo(a):
        if a == "yes":
            print "OK!"
        else:
            loop = 0    # loop is local to function redo
    
    loop = 1     # this loop has global scope
    choice = 0
    Please follow the link at my signature to learn about [c o d e] tags. Thank you for the dots! Appreciated.

    The easiest solution is
    Code:
    def redo(a):
        global loop
        if a == "yes":
            print "OK!"
        else:
            loop = 0    # loop is global
    A better solution, because redo doesn't need to know details of its environment, is


    Code:
    def redo(a):
        if a=='yes':
            print('OK!')
            return True
        return False
    #...
    
        loop = redo(raw_input('question').lower())
    Last edited by b49P23TIvg; January 8th, 2013 at 07:35 AM. Reason: screwy!
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Thank you, one more question


    Thank you for your help! I have one question to your reply. When you type global before any variable, does that mean that the changes made to that variable within that function apply to the program as a whole? and without it, the changes made to that variable won't be applicable to the rest of the program?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481

    global & local scope


    The output of these functions depends on the order in which you try them. The interesting experiment is, perhaps,
    >>> G
    'global'
    >>> global_G()
    global
    >>> G
    'changed'
    Code:
    G = 'global'
    
    def no_assignment_python_finds_G_in_global_scope():
        print(G)
    
    def UnboundLocal_error():
        '''
            G is local because it is assigned to within this function
        '''
        print(G)    # G doesn't yet have a value!
        G = 8       # assigment makes G local
    
    
    def global_G():
        '''
            G in this function is the global G
        '''
        global G                             # tell python where to find G
        print(G)
        G = 'changed'
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo