473,397 Members | 1,972 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,397 software developers and data experts.

Create a list of playing card specs and store in a file [solved]

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, where each line in the file specifies a single card with the rank and then the suit separated by a space. Hint: sort the list first by rank, and then by suit.


thanx
Oct 22 '06 #1
3 1780
bartonc
6,596 Expert 4TB
C'mon! We're not going to do you work for you.
Oct 22 '06 #2
C'mon! We're not going to do you work for you.
This is not a class work or university assignment..it is my own work..actually i am learning python by my own..these questions are taken from a book, i solved all but these questions are giving me trouble..thank you that you help me on other questions but u atleast can give me some idea

thank you
Oct 22 '06 #3
bartonc
6,596 Expert 4TB
This is not a class work or university assignment..it is my own work..actually i am learning python by my own..these questions are taken from a book, i solved all but these questions are giving me trouble..thank you that you help me on other questions but u atleast can give me some idea

thank you
Ok. It's awesome for me to see the progress that you are making. Learning does happen when you are willing to experiment with examples.
This forum is here for this reason. Here are two more functions so you can see how files are used. Ultimately, we don't use files for storage so much anymore, now that SQL databases are free tools for data storage.

Expand|Select|Wrap|Line Numbers
  1. rank_desc = {1:"Ace", 2:"Two", 3:"Trey", 4:"Four",
  2.              5:"Five", 6:"Six", 7:"Seven"}    # describe each rank in a dictionary
  3. suit_desc = {"s":"Spades", "h":"Hearts"}    # describe each suit in a dictionary
  4. class PlayingCard:
  5.     def __init__ ( self, rank, suit ): # Creates a card.
  6.         self.rank = rank
  7.         self.suit = suit
  8.     def getRank(self): #Returns the rank of the card.
  9.         return self.rank
  10.     def getSuit(self): # Returns the suit of the card. 
  11.         return self.suit
  12.     def BJValue(self): # Returns the 'Blackjack value' of the card (Ace;1, Face card:10) 
  13.         return min(self.rank, 10)
  14.     def __str__(self): # Returns a string naming the card. For example: 'Ace of Spades'
  15.         return "The %s of %s is worth %d Blackjack" %(rank_desc[self.rank],
  16.                                                  suit_desc[self.suit],
  17.                                                  self.BJValue())
  18. def WriteCardDatabase(filename):
  19.     """"Create a random collection of card specs.
  20.     Dictionaries work great because they are not ordered lists."""
  21.     outputList = [] # an empty list for temporary storage
  22.     for rank, r_desc in rank_desc.items():
  23.         for suit, s_desc in suit_desc.items():
  24.             line = str(rank) + " " + suit + "\n"
  25.             outputList.append(line)
  26.     outputFile = file(filename, "w") # open or create a file in "w"rite mode
  27.     outputFile.writelines(outputList)
  28.     outputFile.close()
  29. def ReadCardDatabase(filename):
  30.     inputFile = file(filename, "r")    # read mode
  31.     inputList = inputFile.readlines()
  32.     inputFile.close()
  33.     return inputList
  34. def CreateDeckOfCards(cardspecs):
  35.     cardList = []
  36.     for item in cardspecs:
  37.         cardspec = item.split()
  38.         rank, suit = int(cardspec[0]), cardspec[1]
  39.         cardList.append(PlayingCard(rank, suit))
  40.     return cardList
  41.  
  42. WriteCardDatabase(r"C:\WINDOWS\Temp\cardlist.txt")
  43. cardspecList = ReadCardDatabase(r"C:\WINDOWS\Temp\cardlist.txt")
  44. # now that you have a list, you can sort it...
  45. myDeck = CreateDeckOfCards(cardspecList)
  46. for card in myDeck:
  47.     print card
  48. ##AceOfSpades = PlayingCard(1, 's')
  49. ##
  50. ##print AceOfSpades
  51. ##print AceOfSpades.getRank()
  52.  
Oct 22 '06 #4

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

Similar topics

4
by: Brian Basquille | last post by:
Hello all, What is the syntax for simply playing a WAV file in your program? Am learning VB and have two VB books but don't wanna root through them to find the code. Any help would be much...
7
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif);...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
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...
1
by: prince99 | last post by:
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,...
9
by: Cjllewey369 | last post by:
I don't know how clear my title is, but I can't think of how to title this problem. I am writing a single card game of War. It is very simple. Every round the deck is populated and shuffled. A single...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
5
by: slither101 | last post by:
Hi, I have been working on this project for about three days now, and seem to be stuck on my conditional statements that specify the face and suit of the card when it is a Jack, Queen, King and Ace....
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.