4 Replies - 1805 Views - Last Post: 29 November 2012 - 11:59 AM Rate Topic: -----

#1 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Methods will not work within function

Posted 28 November 2012 - 10:16 PM

Im writing a main function using classes from another file, and when I put the code in a function it does not work, but when I leave it out it works fine. For Example I had
from card import *
from CardCollection import *




def main():
    Player1 = input("Player 1, what is your name?: ") # Gets the first players name
    print("Ok, thanks " + Player1) # Shows them that I got their name
    Player2 = input("Player 2, what is your name?: ")
    print("Got it, good luck " +  Player2)
    playingDeck = CardDeck() # Generates a deck of 52 cards
    playingDeck.shuffle() # Uses the shuffle method to put the cards in a random order
    Player1hand = playingDeck.deal(15) # Deals out to Player1
    Player2hand = playingDeck.deal(15) # Deals out to Player2



This will not work, but when I change it to this it works fine...
from card import *
from CardCollection import *



playingDeck = CardDeck() # Generates a deck of 52 cards
playingDeck.shuffle() # Uses the shuffle method to put the cards in a random order
Player1hand = playingDeck.deal(15) # Deals out to Player1
Player2hand = playingDeck.deal(15) # Deals out to Player2


def main():
    Player1 = input("Player 1, what is your name?: ") # Gets the first players name
    print("Ok, thanks " + Player1) # Shows them that I got their name
    Player2 = input("Player 2, what is your name?: ")
    print("Got it, good luck " +  Player2)



Is This A Good Question/Topic? 0
  • +

Replies To: Methods will not work within function

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Methods will not work within function

Posted 29 November 2012 - 04:35 AM

Well... You never actually call your main function.

def main():
    # Your code here

if __name__ == "__main__":
    main()


The if statement simply ensures that the file is not being used as a module. __name__ is an attribute which is automatically set - if you double click on your python script (or run it from IDLE or whatever) it will be set to the string "__main__". However, if you were to import your script as a module in another script, it will be set to something else.
Was This Post Helpful? 2
  • +
  • -

#3 Stephenfrazier10  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 109
  • Joined: 12-September 12

Re: Methods will not work within function

Posted 29 November 2012 - 11:41 AM

I might of phrased it wrong, when I run the first one with the code outside of the main function it all works. However when I put that code inside of the main function, it acts like it never creates the CardDeck object. When I run it, then type myDeck in the Python shell it says that it is undefined, but when I leave it ouside of the function it says that it is a cardDeck object.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Methods will not work within function

Posted 29 November 2012 - 11:48 AM

I think Python_4_President explained it pretty well in your other thread: By default all variables you create in a function are local to that function. So if you create a variable in main and then try to access that variable after main has finished running, it won't work because the variable only exists as long as main is still running.

You can "fix" that by using the global keyword to make your variables global, but you should avoid that since using global variables is generally bad practice.

As long as you put all the code that accesses the variables inside the main function or inside other functions that take the values as arguments, all should be fine.
Was This Post Helpful? 1
  • +
  • -

#5 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Methods will not work within function

Posted 29 November 2012 - 11:59 AM

Did you read my response in the other thread?

I mean, I know that was an "accidental" thread, but I told you what was going on in it...



Just in case you didn't understand, here are more examples dealing with scope.

module_local_variable = True

def module_local_function1():
    function_local_variable = module_local_variable

def module_local_function2():
    return module_local_variable

def module_local_function3():
    function_local_variable = False
    return function_local_variable





>>> module_local_variable
True
>>> function_local_variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function_local_variable' is not defined
>>> module_local_function1()
>>> module_local_function2()
True
>>> module_local_function3()
False
>>> function_local_variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function_local_variable' is not defined
>>> 




As you can see, module_local_variable is visible in all the functions which are local to the module.

function_local_variable is ONLY visible to the function which holds its definition.


if you had two functions like so:
def func1():
    a = True

def func2():
    a = False 


the variable a in each function is independent of the other. Neither are accessible outside the scope of the function in which they are defined.

You can return the value of that variable (as seen in module_local_function3), but you can't access it directly,


So you should be asking yourself now:
Do I need to define all that stuff in the main() function, or outside of it? The answer will be dependent on what you intend to do in the main() function.. IE, everything, or just the "gamey" parts of the game (like dealing and managing hands)?
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1