count method in 'if' doesn't work - python -
i don't it, i'm trying count 2 in list , when this:
hand=['d2', 'h5', 's2', 'sk', 'cj', 'h7', 'cq', 'h9', 'd10', 'ck'] f=''.join(hand) count2=f.count('2') print count2
it works , prints me 2 number of times 2 in list. when i'm putting in if doesn't work:
def same_rank(hand, n): if hand.count('2')>n: print hand.count('2') else: print 'bite me' hand=['d2', 'h5', 's2', 'sk', 'cj', 'h7', 'cq', 'h9', 'd10', 'ck'] f=''.join(hand) n=raw_input('give n ') print same_rank(hand,n)
if user gives n=1 supposed print 2 because number 2 twice in list , want more 1 is! why doesn't return that?
raw_input()
returns string; strings sorted after numbers, 2 > '1'
false:
>>> 2 > '1' false
convert input integer first:
n = int(raw_input('give n '))
had used python 3, you'd have gotten exception instead:
>>> 2 > '1' traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: unorderable types: int() > str()
because python 3 has done away giving arbitrary types relative ordering.
next, don't pass in f
, passing in hand
, list:
>>> hand.count('2') 0 >>> f 'd2h5s2skcjh7cqh9d10ck' >>> f.count('2') 2
you wanted pass in latter, function doesn't work otherwise.
Comments
Post a Comment