0

I'am creating a discard method that remove one or several cards(indicated by the user) in a hand and replace them with card from the deck.

I had a list of all the cards in my list and I created a window, the buttons, and an Entry box.

What I planned to do was to take the input from the entry box and replace the card indicated with a random card;then return the hand.

However, my remove_card function does not seem to work.I am guessing the getText function did not get the input correctly.

from graphics import*
suits = ["c","d","h","s"]
ranks=["a","2","3","4","5","6","7","8","9","t","j","q","k"]
list=[]
x=0
for items in ranks:
    list.append(ranks[x]+suits[0])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[1])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[2])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[3])
    x=x+1

#creat a list of all the card#

import random
hand=random.sample(list,5)

#select 5 random cards#

def shuffle(hand):
        x=50
        for cards in hand:
                i=Image(Point(x,100),"C:/ICS/cards/"+cards+".gif")
                i.draw(w)
                x=x+20
#a function that prints card#

def remove_card(cards):
    g=[]
    inputStr=inputBox.getText()
    for card in cards:
        if card==inputStr:
            card=cards.replace(inputStr,random.choice(list))
            g.append(card)
        else:
            g.append(card)
    return g


from graphics import *
w=GraphWin('My Window', 400, 200)
i=Point(100,100)
aRectangle=Rectangle(Point(10,150),Point(100,190))
aRectangle.setFill('green')
message=Text(Point(55,170),"Deal")
aRectangle2=Rectangle(Point(150,150),Point(240,190))
aRectangle2.setFill('red')
aRectangle3=Rectangle(Point(150,10),Point(250,50))
message2=Text(Point(195,170),"Quit")
aRectangle.draw(w)
aRectangle2.draw(w)
aRectangle3.draw(w)
message.draw(w)
message2.draw(w)
#drawing all the basics of the deck#
hand=random.sample(list,5)
shuffle(hand)#shuffle cards#
remove=Text(Point(340,130),"Chance to Discards")
remove.draw(w)
inputBox=Entry(Point(350,150),20)
inputBox.draw(w)
hand=remove_card(hand)

while True:
        p3=w.getMouse() #if the point is in the range of quit, close the window#
        if p3.getX()>150 and p3.getX()<240 and p3.getY()>150 and p3.getY()<190:
                w.close()
                break
        elif p3.getX()>10 and p3.getX()<100 and p3.getY()>150 and p3.getY()<190:
                hand=random.sample(list,5)#if the point is inside the range of deal, deal card#
                shuffle(hand) #change hand#
cdlane
  • 40,441
  • 5
  • 32
  • 81

1 Answers1

0

remove_card function does not seem to work.I am guessing the getText function did not get the input correctly.

No, your code to test cards.replace() is broken and this line of remove_card() doesn't work:

card=cards.replace(inputStr,random.choice(list))

there is no .replace() method for the type list and you don't need it anyway. Fixing this function, and replacing the variable list with card_list:

def remove_card(cards):
    hand = []

    text = inputBox.getText()

    for card in cards:
        if card == text:
            hand.append(random.choice(card_list))
        else:
            hand.append(card)

    return hand

Hand test this code, don't rely on your current testing code.

You can replace all four of these similar loops:

x=0
for items in ranks:
    list.append(ranks[x]+suits[0])
    x=x+1

with just one loop (again list -> card_list):

for suit in suits:
    for rank in ranks:
        card_list.append(rank + suit)
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Thank you so much! – Jane May 20 '17 at 00:07
  • @Jane, you're welcome. Note that this `remove_card()` function is flawed in the same way as your hand allocation (random sample) code -- they don't remove the card(s) they select from `card_list` so a card might get allocated anew when it shouldn't. It seems a relatively easy problem to fix, however. – cdlane May 20 '17 at 05:06