473,379 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Using class to hold playing card data [solved]

Implement a class that represents a playing card. The class should implement the following methods:
__init__ ( self, rank, suit ) Creates a card.
rank is an integer in the range of 1 to 13 (Ace:1, King:13), suit is a character in the set { 'h', 'd', 'c', 's' }.
getRank(self) Returns the rank of the card.
getSuit(self) Returns the suit of the card.
BJValue(self) Returns the 'Blackjack value' of the card (Ace;1, Face card:10)
__str__(self) Returns a string naming the card. For example: 'Ace of Spades'
Note: __str__ is a special method in Python that is used when a program asks for a string representation of an object.
Test your class by writing a 'test harness' that generates n randomly generated cards, where n is supplied by the user. Print out the string associated with each card and it's 'Blackjack value'.


any comments welcome
Oct 22 '06 #1
1 2851
bartonc
6,596 Expert 4TB
You actuall did most of the work here
Expand|Select|Wrap|Line Numbers
  1. rank_desc = (None, "Ace", "Two",)    # describe each rank in a tuple
  2. suit_desc = {"s":"Spades", "h":"Hearts"}    # describe each suit in a dictionary
  3. class PlayingCard:
  4.     def __init__ ( self, rank, suit ): # Creates a card.
  5.         self.rank = rank
  6.         self.suit = suit
  7.     def __str__(self): # Returns a string naming the card. For example: 'Ace of Spades'
  8.         return "The %s of %s is worth %d in Blackjack" %(rank_desc[self.rank],
  9.                                                  suit_desc[self.suit],
  10.                                                  self.BJValue())
  11.     def getRank(self): #Returns the rank of the card.
  12.         return self.rank
  13.     def getSuit(self): # Returns the suit of the card. 
  14.         return self.suit
  15.     def BJValue(self): # Returns the 'Blackjack value' of the card (Ace;1, Face card:10) 
  16.         return min(self.rank, 10)
  17.  
  18. AceOfSpades = PlayingCard(1, 's')
  19. print AceOfSpades
  20. print AceOfSpades.getRank()
  21.  
Oct 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PaJeeper | last post by:
I'm trying to write code that will automatically add a specified number of records to a table using variables derived from queries. I am attempting to do this with DAO. Background. I have...
2
by: Vinay | last post by:
Hi, I want to verify if a particular song is being played from my program. I have read a lot of examples of using winmm.dll and importing PlaySound(). If there anyway to check if a song is being...
3
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign...
0
by: bohuge | last post by:
Hey! At the time being I'm working on a backup solution for a Qtek9090 pocketpc, which should be able to find and backup outlook data to a server, local files, messages and contact from the sim...
2
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't...
3
by: prince99 | last post by:
Write a program that creates a list of card objects and prints out the cards grouped by suit and in rank order within each suit. The program should read the values for the list of cards from a file,...
1
by: CapMaster | last post by:
I've found some programs of how to create a standard game of blackjack on C++. But how would you do it using structs? Here is my assignment: Problem Statement: The purpose of this project is to...
9
by: viz | last post by:
hi, i have written a class for session handling, and i want to use it to keep track of the user. After authenticating the user in login page i am storing the session info like uname etc.. in a...
3
by: dave | last post by:
Hello, I'm currently on the class section of my self-taught journey and have a question about classes: is it possible to bring a object created inside the class definitions outside the class so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.