The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Variable seems to reset itseld
Discuss Variable seems to reset itseld in the Python Programming forum on Dev Shed. Variable seems to reset itseld Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 19th, 2012, 06:01 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
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')
|

November 19th, 2012, 07:59 AM
|
 |
Contributing User
|
|
|
|
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!
|

November 19th, 2012, 03:51 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 25 m 39 sec
Reputation Power: 0
|
|
|
Ummm... I think I followed that.
So I can't do it?
|

November 19th, 2012, 04:16 PM
|
 |
Contributing User
|
|
|
|
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)
|

November 20th, 2012, 01:50 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|