Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 06:01 AM
howsono howsono is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 howsono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 39 sec
Reputation 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')

Reply With Quote
  #2  
Old November 19th, 2012, 07:59 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,351 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 46 m 12 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 19th, 2012, 03:51 PM
howsono howsono is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 howsono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 39 sec
Reputation Power: 0
Ummm... I think I followed that.

So I can't do it?

Reply With Quote
  #4  
Old November 19th, 2012, 04:16 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,351 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 46 m 12 sec
Reputation Power: 383
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)

Reply With Quote
  #5  
Old November 20th, 2012, 01:50 AM
howsono howsono is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 howsono User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 39 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Variable seems to reset itseld

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap