Help with simple python code

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 613 Views - Last Post: 27 February 2014 - 08:30 PM Rate Topic: -----

#16 thrgk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 26-February 14

Re: Help with simple python code

Posted 26 February 2014 - 10:41 PM

so our range((len(deck)) is already specified as 0-52, so cant we use randrange(1en(deck))-1 somehow to pick a RANDOM card and subtract it?
Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: Help with simple python code

Posted 26 February 2014 - 11:05 PM

View Postthrgk, on 27 February 2014 - 12:41 AM, said:

so our range((len(deck)) is already specified as 0-52, so cant we use randrange(1en(deck))-1 somehow to pick a RANDOM card and subtract it?



Yes, randrange is the correct function to use here. I'm not sure what "somehow" means, but it looks like you should try implementing your idea, and then test it very carefully. If it doesn't fail, it may be correct.

(how do you test an implementation this function? Interesting question... worth thinking about... testing is important stuff for programmers...)
Was This Post Helpful? 0
  • +
  • -

#18 thrgk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 26-February 14

Re: Help with simple python code

Posted 27 February 2014 - 11:23 AM

ok so i think I got it.

def print_cards(clist):
"""Print cards in list clist.

Precondition: clist is a list of Cards, possibly empty."""
for c in clist:
# TODO: fix this line so it prints card c (or str©)
print c

def draw(deck):
"""Returns: a card that is randomly drawn (and removed) from deck.

Precondition: deck is a list of cards containing at least one card."""
i = random.randrange(len(deck)) # random int in 0..len(deck)-1
# TODO: add a line make this function obey its specification
# You will, of course, want to use the value of i.
# This function is used in draw_poker_hand, below.

t = clist[i]
return t.remove(i)

we called range function in i= and i then want to get a card, not just a number, "i" will return the value like 23, but I need jack of diamonds(example) so clist[i] should do that, but for some reason it says clist is not defined, and it is in the above definition. I then want to return "t" and ALSO remove it from the deck so I need the remove function. but why does it say clist not defined?
Was This Post Helpful? 0
  • +
  • -

#19 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Help with simple python code

Posted 27 February 2014 - 12:19 PM

It's basically a case of mistaken identity. You have gotten away with it because you are calling it clist everywhere but with your draw function, you called it deck so that is what you must work with. Your deck is the clist. It's just named differently now.

Now, if you pretend that this code is already "in the wild" as in people are using it, then you cannot just change the signature of the function to your heart's content*. It's important that you stick to the public representation of the code which is def draw(deck).

The fix here is to change clist to deck.

* The reason you cannot just change it is because if someone wrote:

cardstuff.draw(deck=clist)



your code would be broken.

Here is a more succinct example of that:

def break_me(data):
    print data
    
break_me(data="this is a test")



When I change my function to use the word message instead:

def break_me(message):
    print message
    
break_me(data="this is a test")



output said:

brandon@mothership:~/pythons$ python throwie.py
Traceback (most recent call last):
File "throwie.py", line 4, in <module>
break_me(data="this is a test")
TypeError: break_me() got an unexpected keyword argument 'data'


So, that should provide some rationale as to why you should not just change def draw(deck) to def draw(clist) which would work (for now) but is not safe.

And with that, hopefully you understand the differences in naming.
Was This Post Helpful? 1
  • +
  • -

#20 thrgk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 26-February 14

Re: Help with simple python code

Posted 27 February 2014 - 07:57 PM

I think I got it, does this look ok?




def draw(deck):
"""Returns: a card that is randomly drawn (and removed) from deck.

Precondition: deck is a list of cards containing at least one card."""
i = random.randrange(len(deck)) # random int in 0..len(deck)-1
# TODO: add a line make this function obey its specification
# You will, of course, want to use the value of i.
# This function is used in draw_poker_hand, below.

t = deck[i]
return deck.remove(t)

I made t=deck[i] instead and then returned deck.remove(t) to remove the card picked from the deck?
Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: Help with simple python code

Posted 27 February 2014 - 08:10 PM

You really should be running your tests before you ask us whether your function works. You have these tests, they're there for you to use. Why aren't you using them?
Was This Post Helpful? 0
  • +
  • -

#22 thrgk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 26-February 14

Re: Help with simple python code

Posted 27 February 2014 - 08:11 PM

not really sure how to do the testing, i implement it into my code, and then run it through interactive python?
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7760
  • View blog
  • Posts: 13,124
  • Joined: 19-March 11

Re: Help with simple python code

Posted 27 February 2014 - 08:30 PM

Testing is a pretty important skill for a programmer. Fortunately, you have a test suite as I recall. You could start with that.

Beyond that, you can think about what the world should look like after you draw a card from a deck, and then think about how you check to see if the world looks like that.

So for example, if I draw a card from a deck, I expect to have a card in my hand. When you run this, do you get back a card? I also expect that the deck will have one less instance of this card than it did when I started. (so if there's five Aces of Spades to start, and I draw one, there should be four) How can you test this? Well, suppose you look at the deck you started with, and then you look at the deck created by putting the card you drew back into the deck you drew it from. They should be the same, right?

If you pass those tests, you might have some justified confidence that you've drawn a card successfully. If not, you might want to go back and think about what went wrong.

There's more things you might want to be curious about here. For example, how do you test if you're actually getting a card at random? I encourage you to think about that a little, but we're not going to get into testing that here.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2