Pythonidae
Pythonidae

Reputation: 11

Returning a given name corresponding to a random integer

Disclosure: I'm a Python (& coding) infant. I just started CS, I'm doing my best but I'm struggling. This is a homework problem. I'm assigning a card suit based on a randomly generated integer (from 0 to 3), s.t. 0 = Spades, 1 = Hearts, 2 = Clubs, and 3 = Diamonds.

Here is what I'm given:

def random_suit_number():
    ''' Returns a random integer N such that 0 <= N < 4. '''
    pass

def get_card_suit_string_from_number(n):
    ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    pass

And here is the (sad, sad) point I'm at:

def random_suit_number():
''' Returns a random integer N such that 0 <= N < 4. '''
    return random.randint(0, 3)

def get_card_suit_string_from_number(n):
''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()
    if n == 0: 
        get_card_suit_string_from_number(n) = 'Spades'

Can someone please logic me through this? It's obviously not finished, Repl's telling me "get_card_suit_string_from_number(n) = 'Spades'" is invalid syntax; it's taken me hours to get to this point so I'm really dragging my teeth on cement right now.

Upvotes: 1

Views: 111

Answers (4)

DrD
DrD

Reputation: 561

courage! your first function is actually correct as far as i can see...

for the second one:

def get_card_suit_string_from_number(n):
''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''

since n is given in parentheses after the function name in the declaration it means it is already defined within the function. In fact it is the input given to the function, so you don't need to assign it yourself, although the caller of your function would have probably assigned it the way you did here:

    n = random_suit_number() #not needed

An if clause is good, you want to check for valid numbers, but valid numbers are 0,1,2,3 you could formulate the condition n in [0,1,2,3]

    if n == 0: #put the correct condition here

The way to return a value from the function is via a return statement (like you correctly did for function 1). In fact you want two return statements, one in the first branch of the if statement and one in the else branch that you also have to create. Have a look at dictionaries to assign the correct suit string per n!

        #define dict
        get_card_suit_string_from_number(n) = 'Spades' #change to return statement
    #add else branch with second return

Good luck, you're almost there!

Upvotes: 0

DarrylG
DarrylG

Reputation: 17166

You're close. You can expand your function as follows.

def get_card_suit_string_from_number(n):
    ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()

    if n == 0: 
        return 'Spades'
    elif n == 1:
        return 'Hearts'
    elif n == 2:
        return 'Clubs'
    elif n == 3:
        return 'Diamonds'
    else:
        return None

Upvotes: 1

Saleem Ali
Saleem Ali

Reputation: 1383

Just map the value with name in dict:

def get_card_suit_string_from_number(n):
   ''' Returns the name of the suit that corresponds to the value n, or None if n is an invalid number. '''
    n = random_suit_number()
    return {
        0: 'Spades',
        1: 'Hearts',
        2: 'Clubs',
        3: 'Diamonds',
    }.get(n)

Upvotes: 0

Laksh Matai
Laksh Matai

Reputation: 176

You basically want to return just the name, so just return 'Spades' or 'Clubs'. Basically after you got your random number n you just compare it's value with 0,1,2 and 3 and do return 'Clubs'

Upvotes: 0

Related Questions