2 Replies - 1515 Views - Last Post: 29 November 2012 - 12:47 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:15 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



Is This A Good Question/Topic? 0
  • +

Replies To: Methods will not work within function

#2 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 28 November 2012 - 10:21 PM

I'm guessing (because you only posted a little code with lots of errors in it) you're having trouble because playingDeck is local to the scope of the main function. Not outside of it.

>>> def main():
...     thing_local_to_main = True
... 
>>> main()
>>> thing_local_to_main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'thing_local_to_main' is not defined




and Player1hand and Player2hand aren't ever defined.

This post has been edited by Python_4_President: 28 November 2012 - 10:22 PM

Was This Post Helpful? 1
  • +
  • -

#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 - 12:47 AM

This was an accidental post

Look at my other post with the same title if you can
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1