Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

Hello. I am trying to figure out how to uses lists and class methods together for object oriented programming. Can anyone explain how to do this? I know how to create a class: "class Card(object): def__init__(self, rank, suit): self.rank = rank self.suit = suit" And, a list: "rank = {'Ace':1, 'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13}" Now how do I do this in Python using this list in the above class method?

OpenStudy (anonymous):

o mg yes

OpenStudy (rsmith6559):

First, that's not a list, it's a dictionary. Classes are blueprints for objects, they don't really exist in your program. You use classes to create objects (instances) of a class. The object is what you work with in your program. If I want a card object, I would: myCard = Card( "Ace", "Spade" ) This creates a Card object named myCard and runs the __init__() function to initialize myCard to be the ace of spades. To run methods of myCard, my code would be: print myCard.__str__() (You don't have any of your own methods in the post for me to use) This invokes the __str__() method on the myCard object.

OpenStudy (anonymous):

I ended up working something out that involved a lot of elif statements and while it could be written better I'm still learning and this is just the best that I can do at this time. Thank you for the help! I was going to send you a private message but you have that turned off for some reason, but I do have another question that I would prefer to ask privately if you don't mind re-enabling your PM? Thanks!

OpenStudy (rsmith6559):

It's on.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Countless7Echos: Ah trying out the whole T.V girl drawing :p (I love drawing eyes)
2 hours ago 7 Replies 5 Medals
kaelynw: starting to draw a hand
1 day ago 16 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
2 days ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
2 hours ago 7 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
3 days ago 13 Replies 1 Medal
kaelynw: tried a lil smt, the arm is off but i like the other stuff
3 days ago 27 Replies 3 Medals
kaelynw: art igg
3 days ago 14 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!