We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,965 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

write a program for making a deck

I need help writing this program, any hints, help, guidance will be appreciated.
The program needs to provide a menu to the user with the following options:

Reset the deck
Shuffle the deck
Cut the deck
Cut to a card.
Cut against the computer
Exit

Each of these options must work. You must include at least one of each of the following:

Decision structure
Repetitive structure
module/function
array (if you use one in your object it will count)
Object (The one in Exercise 11 counts)
3
Contributors
5
Replies
18 Hours
Discussion Span
9 Months Ago
Last Updated
6
Views
Iceman10284
Newbie Poster
14 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Looks pretty standars stuff found in many codes in our Code Snippet section. Cut is usually not done but it would be simple to do:

Determine cut_point
deck = deck[cut_point:] + deck[:cut_point]
pyTony
pyMod
Moderator
6,305 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
#Jason Edgel#
#Program for deck of cards#

class deck_of_cards:
    #Making the deck#
    def __init__(self):
        self.__cards = []
        self.resetdeck()

    #Reset the Deck#
    def resetdeck(self):
        del self.__cards[0:]
        for suit in range(1,5):
            for value in range(1,14):
                self.__cards.append(str(value)+"|"+str(suit))

    #Shuffle the Deck#
    def shuffle(self):
        random.shuffle(self.__cards)

    #Cut the Deck#
    def cutdeck(self):
        cutdeck1 = self.__cards[:26]
        cutdeck2 = self.__cards[26:]
        self.__cards = cutdeck2 + cutdeck1

    #Cut to a card#
    def cuttocard(self):
        self.cutdeck()
        print self.__cards[-1]

    #Cut to the highest card and have it win#
    def cutCPU(self):
        playerTop = self.__cards[-1]
        del self.__cards[-1]
        compTop = self.__cards[-1]
        del self.__cards[-1]
        print "Player", "Computer"
        print ""
        print playerTop," ", compTop

    #Return to cards#
    def get_cards(self):
        return self.__cards

    #Program that uses the class I made above#

    import random

    def main():
        getcard = deck()
        menuloop = 1
    #Menu loop#
        while menuloop == 1:
            print ""
            print "1. Reset the deck"
            print "2. Shuffle the deck"
            print "3. Cut the deck"
            print "4. Cut to a card"
            print "5. Cut against the computer"
            print "6. Exit the program"
            print ""
uM = input("Select a menu option(#): ")
print ""
if uM == 1:
    getcard.resetdeck()
    print getcard.get_cards()

    if uM == 2:
        getcard.shuffle()
        print getcard.get_cards()

    if uM == 3:
        getcard.cutdeck()
        print getcard.get_cards()

    if uM == 4:
        getcard.cuttocard()

    if uM == 5:
        getcard.cutCPU()

    #End the loop#
    if uM == 6:
        print "Thanks for playing"
        menuloop = 0

    #Defensive if statement, only accepts #'s 1-7#
    if uM != 1 and uM != 2 and uM != 3 and uM != 4 and uM != 5 and uM != 6 and uM != 7:
        print "Wrong input entered"

main()
Iceman10284
Newbie Poster
14 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

here's what I have, I'm getting an error on the menu portion.

Iceman10284
Newbie Poster
14 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You are printing menu by the main function after checking the input, which I find strange, especially since it is endless loop printing the menu. You have also defined this function inside the class with missing self parameter, but you call it like free function. Why are not using else instead of separate if at end? Also the alternatives are exclusive and should be elif in single if. deck is not defined in main also and you have no instance of deck_of_cards in your program. By convention the class name should also be in CapitalizedWordsFormat.

Also you have uM tested for other values inside if which ensures uM is 1, so the other if conditions can never be True.

pyTony
pyMod
Moderator
6,305 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
HiHe
Posting Whiz
332 posts since Oct 2008
Reputation Points: 177
Solved Threads: 34
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0661 seconds using 2.67MB