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)

Recommended Answers

All 5 Replies

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]
#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()

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.