Home > Mobile >  Writing the data on the same line
Writing the data on the same line

Time:11-27

I want to write the other card's values in the same line with 'else'. E.g card number 2 equals 2, card number 3 equals 3...

btw from 1 to 13 is from ace to king and 15, 16, 17, 18 are suites.

def get_value(num, suit):
            if (suit == 18 or suit == 15) and num == 7:
                 return -21
            elif suit == 16 and num == 7:
                return -11
            elif num == 1 and (suit== 15 or suit==16 or suit==17 or suit==18):
                return 11
            elif (num == 12 or num == 13) and (suit== 15 or suit==16 or suit==17 or suit==18):
                return 10
            elif num == 11 and (suit== 15 or suit==16 or suit==17 or suit==18):
                return 25

CodePudding user response:

If I understand correctly you want to return the value of the card, where ace, jack, queen and king and 7 of a particular suit returns special values, otherwise just the numeric value.

Assuming the 15-18 are the only valid suits we do not need to check if the suit is one of those if all return the same value, we only need to treat the special cases.

def get_value(num, suit):
    if num==7 and suit in (15,18): # suit is either 15 or 18
        return -21
    elif num==7 and suit==16:
        return -11
    elif num==1: # suit doesnt matter for ace
        return 11
    elif num in (12, 13): # queen or king
        return 10
    elif num==11: # ace
        return 25
    else: # all other cases - return num as value
        return num  

I'm guessing the final else statement is what you were looking for, where the value you get out is just the number on the card in in the general case where none of the special cases trigger, eg get_value(3, 15) would return 3.

CodePudding user response:

I think this is what you want:

def get_value(num, suit):
    return -21 if (suit == 18 or suit == 15) and num == 7 else -11 if suit == 16 and num == 7 else 11 if num == 1 and (suit== 15 or suit==16 or suit==17 or suit==18) else 10 if (num == 12 or num == 13) and (suit== 15 or suit==16 or suit==17 or suit==18) else 25 if num == 11 and (suit== 15 or suit==16 or suit==17 or suit==18) else None

Because this is very messy and chaotic, I think checking against a dict of constraints would be better:

def get_value(num, suit):
    constraints = {((18, 15), (7,)): -21, ((16,), (7,)): -11, ((15, 16, 17, 18), (1,)): 11, ((15, 16, 17, 18), (12, 13)): 10, ((15, 16, 17, 18), (11,)): 25}
    for x in constraints:
        if suit in x[0] and num in x[1]:
            return constraints[x]

A constraint tuple (ex. ((18, 15), (7,)) ) is only fulfilled if its first tuple (ex. (18, 15) ) contains suit and its second tuple (ex. (7,) ) contains num.

Additionally you could return a default value if the args passed in don't match the constraints.

  • Related