1

I'm trying to make this programm and I want from a list that it randomly get generated to find the 2,3,4,...,K,A and if there are more than a number n to return that number. Same for the 3rd def but there I want the C,D,H,S and then return how many they are. But all I'm getting as a result from the 2nd def is none. What do I have to fix to make it work?

Here is the code if it helps anyone

import random
def make_deck():
    suits=['C', 'D', 'H', 'S']
    ranks=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    deck = [s+r for s in suits for r in ranks]
    random.shuffle(deck)
    return deck


def same_rank(hand, n):
    if hand.count('2')>n:
        count2=hand.count(2)
        return count2
    else:
        return 'None'
    if hand.count(3)>n:
        count3=hand.count(3)
        return count3
    else:
        return 'None'
    if hand.count(4)>n:
        count4=hand.count(4)
        return count4
    else:
        None
    if hand.count(5)>n:
        count5=hand.count(5)
        return count5
    else:
        return 'None'
    if hand.count(6)>n:
        count6=hand.count(6)
        return count6
    else:
        return 'None'
    if hand.count(7)>n:
        count7=hand.count(7)
        return count7
    else:
        return 'None'
    if hand.count(8)>n:
        count8=hand.count(8)
        return count8
    else:
        return 'None'
    if hand.count(9)>n:
        count9=hand.count(9)
        return count9
    else:
        return 'None'
    if hand.count(10)>n:
        count10=hand.count(10)
        return count10
    else:
        return 'None'
    if hand.count('J')>n:
        countJ=hand.count('J')
        return countJ
    else:
        return 'None'
    if hand.count('J')>n:
        countQ=hand.count('Q')
        return countQ
    else:
        return 'None'
    if hand.count('K')>n:
        countK=hand.count('K')
        return countK
    else:
        return 'None'
    if hand.count('A')>n:
        countA=hand.count('A')
        return countA
    else:
        return 'None'


def same_suit(hand):
    if hand.count('C')>0:
        countC=hand.count('C')
    if hand.count('D')>0:
        countD=hand.count('D')
    if hand.count('H')>0:
        countH=hand.count('H')
    if hand.count('S')>0:
        countS=hand.count('S')
    return countC, countD, countH, countS


hand = make_deck()[:10]
print hand
n=raw_input('Give n')
print same_rank(hand,n)

3 Answers 3

1

There is a problem here:

def same_rank(hand, n):
    if hand.count('2')>n:
        count2=hand.count(2)
        return count2
    else:
        return 'None'
    #... 
    #Nothing here will matter!
    #...

In your first if case, one of two two things is going to happen. Either the function will return count2 or it will return 'None'. Once a function returns, it is done and no more code in the function will run. So none of the other if/else cases will matter because the first if/else handles all possibilities, then terminates.

You want a structure more like:

if condition 1:
    ...
elif condition 2:
    ...
...
elif condition10:
    ...
else:
    return None

Edit: There are also other programming/logic errors in the code. One of your cases says

else:
    None

which doesn't actually do anything.

Returning the string 'None' as opposed to the python null object None is also likely a mistake.

Sign up to request clarification or add additional context in comments.

5 Comments

I put all of them if because I wanted to check all of them, I know that if one of them is true it will not continue checking if others are true and there is a chance for one of the next ones to be true too.
@RedRose23 The issue is not that it stops checking the others once one is true. It stops checking after the first check no matter what! The function will return regardless of whether hand.count('2')>n is true or false because of the first else case.
I did the alteration that you said plus I improved the code so right now it's like you said only with if because I can't make it check everything with elif (I'm not getting any errors) and right now the only problem is that it keeps returning None at the end and I don't want that. Is there any way to remove it? (I put return if front of it and I delete it too but it's the same, even if I erase the else still returns None)
Its hard for me to tell what your code looks like now just based on your description of the changes. If your code still isn't doing what you intend you'll need to debug. If you're not entering an if case that you think you should be entering, put some prints in and see whats wrong. Also consult How To Debug Small Programs
@RedRose23 his alteration does not completely fix your problem. hand.count('2') > n will always evaluate to False. See me answer for a working solution, and an explanation of why your function always returns None. Could you put up your altered code if your still having problems after having read the link @turbulencetoo gave you and my answer?
0
# to count ranks, rank should be a string:
def rank_counter(hand, rank):
    return sum(1 for card in hand if card[1:] == rank)

# finds the first rank that has at least n cards in hand
def same_rank(hand, n): 
    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    for rank in ranks:
        count = rank_counter(hand, rank)
        if count > n: return count
    return None

# also create an integer for n:
print same_rank(hand, int(n))

1 Comment

This looks good but maybe some explanation of some of the clever bits would make it more helpful.
0

There are many problems with your same_rank() function:

  1. One if statement's condition is if hand.count('2'), but another if statement's condition is if hand.count(3).
  2. hand is a list of strings, not a string, so attempting to count the number of times a '2' appears will always result in 0, because your cards also have a suit.
  3. At one point you typed None when I think you meant return None.
  4. This may not be a problem, but you return 'None' rather than None.
  5. The code will always get stuck on the first if/else block, because there are return statements in both the if and else clauses. The other if/else blocks have the same problem.

Now to fix your code, take out all the else clauses and just put a return None at the end. That will mean your function will return None only if all the other conditions fail. Then reconsider how you will count the number of cards in the hand that have the same rank. Then consider how you could probably use a for loop instead of 13 if statements. Once these things have been done, the code will probably look like this:

def same_rank(hand, n):
    for rank in list('23456789JQKA').append('10'): # compact version of ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
        count = 0
        for card in hand:
            if card[1:] == rank:
                count += 1
        if count > n:
            return count
    return None

Your same_suit() function has a few problems: if any of the if statements fail, one of the variables you return at the end won't be defined. You also don't need that many if statements, and when you attempt to count the suits of the cards, it won't work (reason mentioned in point 2 above). Working version:

def same_suit(hand):
    counts = {'C': 0, 'D': 0, 'H': 0, 'S': 0} # dictionary that fixes your problem of undefined variables
    for suit in 'CDHS':
        for card in hand:
            if card[0] == suit:
                counts[suit] += 1
    return counts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.