View Single Post
Old 07-21-2014, 07:37 AM   #6
Groundhog
Coordinator
 
Join Date: Dec 2003
Location: Sydney, Australia
My next problem was something called scope - which I am still getting my head around. I wrote a function that calculated the top jumper for each side and assigned it to a variable. The problem was that, after the function finishes, that variable is lost to me and I couldn't access it again.

One way around it was to use the 'global' statement as I did below. I was positive that that was not the best way to do this. I don't know a great deal about OOP, but I know that keeping objects separate and independent of each other is the fundamental 'best practice', and I think this approach violates that. Then I discovered the 'return' statement, which seems the better workaround.

I apologize in advance to anyone with any kind of programming experience as I'm sure the following is probably the least efficient way possible to generate these calculations:

Code:
def TopJumper(home_5_jump, home_5_hgt, home_4_jump, home_4_hgt, away_5_jump, away_5_hgt, away_4_jump, away_4_hgt): home_5_jump_abl = (home_5_jump + home_5_hgt) home_4_jump_abl = (home_4_jump + home_4_hgt) away_5_jump_abl = (away_5_jump + away_5_hgt) away_4_jump_abl = (away_4_jump + away_4_hgt) #global hometopjumper #global awaytopjumper if home_5_jump_abl > home_4_jump_abl: hometopjumper = "PlayerHome5" elif home_4_jump_abl > home_5_jump_abl: hometopjumper = "PlayerHome4" else: hometopjumper = "PlayerHome5" if away_5_jump_abl > away_4_jump_abl: awaytopjumper = "PlayerAway5" elif away_4_jump_abl > away_5_jump_abl: awaytopjumper = "PlayerAway4" else: awaytopjumper = "PlayerAway5" return hometopjumper, awaytopjumper

I then run the function, feeding into it the attributes stored in my player classes earlier, which gives me a variable named TopJumperResults with the best jumper for the home and road team:

Code:
TopJumperResults = TopJumper(PlayerHome5.jump, PlayerHome5.height, PlayerHome4.jump, PlayerHome4.height, PlayerAway5.jump, PlayerAway5.height, PlayerAway4.jump, PlayerAway4.height)

Now, to have them square off...
__________________
Politics, n. Strife of interests masquerading as a contest of principles.
--Ambrose Bierce

Last edited by Groundhog : 07-21-2014 at 07:38 AM.
Groundhog is offline   Reply With Quote