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

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Variable seems to reset itseld


    I'm trying to debug a small section of code with a Y10 student. Trying to figure out why menu option 4 won't exit - the game variable seems to reset itself. Any thoughts?

    main_program.py
    Code:
    #import menu_functions
    from menu_functions import game_menu,modification_menu,changing_wheels_menu,changing_passenger_bay_menu,info_menu,menu_choices
    
    traction_type=0
    passengers=0
    
    #The menu choices_menu_file is where i made the menus link to one another
    
    game=True
    global game
    while(game==True):
        menu_choices()
        print("Game is currently: ",game)
    menu_functions.py
    Code:
    def game_menu():
        global game
        global choice
        choice = ''
        print('MENU')
        print('====')
        print('1] Play game')
        print('2] Modify robot')
        print('3] Information Menu')
        print('4] Exit game')
        while choice not in ['1','2','3','4']:
            choice = input('Make your choice')
    
    def modification_menu ():
        global game
        global choice
        choice = ''
        print ('MODIFICATION MENU')
        print ('=================')
        print ('1] Change Wheels')
        print ('2] Change Passenger Bay')
        print ('3] Back to Main Menu')
        while choice not in ['1','2','3']:
            choice = input('make your choice')
    
    def changing_wheels_menu ():
        global game
        global choice
        choice = ''
        print ('CHANGING WHEELS')
        print ('===============')
        print ('1] Change to Wooden Wheel')
        print ('2] Change to Skis')
        print ('3] Change to Tracks')
        print ('4] Exit to Modification Menu')
        while choice not in ['1','2','3']:
            choice = input('make your choice')
    
    
    def changing_passenger_bay_menu ():
        global game
        global choice
        choice = ''
        print ('CHANGING PASSENGER BAY')
        print ('===============')
        print ('1] Change to a Large bay')
        print ('2] Change to a Medium bay')
        print ('3] Change to a Small bay')
        print ('4] Exit to Modification Menu')
        while choice not in ['1','2','3']:
            choice = input('Make your choice')
    
    def info_menu ():
        global game
        global choice
        choice = ''
        print ('INFORMATION MENU')
        print ('================')
        print ('WHEELS')
        print ('======')
        print ('Wooden Wheels:')
        print ('costs 1 unit of power on grassland')
        print ('costs 2 unit of power on rocks')
        print ('costs 3 unit of power on ice')
        print ('Tracks:')
        print ('costs 3 unit of power on grassland')
        print ('costs 3 unit of power on rocks')
        print ('costs 3 unit of power on ice')
        print ('Skis:')
        print ('costs 3 unit of power on grassland')
        print ('costs 3 unit of power on rocks')
        print ('costs 1 unit of power on ice')
        print ('==================================')
        print ('PASSENGER BAY')
        print ('=============')
        print ('large: costs 2 unit of power and can hold 3 passengers')
        print ('medium: costs 1 unit of power and can hold 2 passengers')
        print ('small: costs 0 unit of power and can hold 1 passenger')
    
    def menu_choices():
        global game
        global choice
        game_menu()
        if choice=="1":
            print ('coming soon!!!')
        elif choice=="2":
            modification_menu()
            if choice=="1":
                changing_wheels_menu()
                if choice=="1":
                    print ('you chose wheels!')
                    traction_type="1"
                elif choice=="2":
                    print ('you chose skis!')
                    traction_type="2"
                elif choice=="3":
                    print ('you chose tracks!')
                    traction_type="3"
            elif choice=="2":
                changing_passenger_bay_menu()
                if choice=="1":
                    print ('you chose a Large bay!')
                    passengers="3"
                elif choice=="2":
                    print ('you chose a Medium bay!')
                    passengers="2"
            elif choice=="3":
                print ('you chose a Small bay!')
                passengers="1"
        elif choice=="3":
            info_menu()
        elif choice=="4":
            game=False
            print("Game is currently: ",game)
            print('Exiting')
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    global v

    tells python to use v of the module level scope. Python hasn't got a galactic scope. If there were a universal scope I suppose one might call it the set of rules a particular version of python must obey.

    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> global a
    >>> a = 'a in __main__ module'
    >>> import p
    >>> import q
    >>> print(a)
    a in __main__ module
    >>> print(p.a)
    1
    >>> print (q.a)
    a in q.py
    >>>
    # p.py
    global a
    a=1


    # q.py
    global a
    a='a in q.py'
    [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
    Nov 2012
    Posts
    5
    Rep Power
    0
    Ummm... I think I followed that.

    So I can't do it?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,955
    Rep Power
    481
    You could access the current value of game.
    Leave menu_functions.py alone.

    Code:
    import menu_functions
    from menu_functions import game_menu,modification_menu,changing_wheels_menu,changing_passenger_bay_menu,info_menu,menu_choices
    
    traction_type=0
    passengers=0
    
    #The menu choices_menu_file is where i made the menus link to one another
    
    while(menu_functions.game):
        menu_choices()
        print("Game is currently: ",menu_functions.game)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Got it! Magic, thanks! I'd tried that, but I think it was the "import menu_functions" and "from menu_functions import x,y,z" bit that was confusing me. Didn't realise you needed both. I'm a competent programmer but learning Python by the skin of my teeth.

IMN logo majestic logo threadwatch logo seochat tools logo