Troubles with global variables

Janko Hauser jhauser at ifm.uni-kiel.de
Sun Oct 14 14:03:43 EDT 2001


Perhaps something else is wrong, the following does show the changes,
and please take this not an elegant example for a selection menu :-).

HTH,
__Janko

#!/usr/bin/env python
import os

a='some string'
b='some other string'

def changea(): 
    global a 
    a = raw_input('What is the new value of "a"? ') 
 
def changeb(): 
    global b 
    b = raw_input('What is the new value of "b"? ') 
 
def menu():
    global a,b
    while 1:
        os.system('clear')
        print 'a = %s\nb = %s\n\n' % (a,b)
        print 'Menu: \n1) Change a: foo\n2) Change b: bar'
        wants=raw_input('type 1 or 2 to change values, q to quit: ')
        if wants == '1':
            changea()
        elif wants == '2':
            changeb()
        elif wants == 'q':
            print 'bye'
            break
        else:
            print 'wrong input'

            

if __name__ == '__main__':
    menu()

-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list