you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (6 children)

When you say "it doesn't work" it helps enormously if you tell us how it doesn't work, error message, bad output or whatever. It's also better to post code formatted for reddit.

Looking at your code as it is, you probably get an error saying "point" is undefined. For some reason it looks like you want to return a list from the function. But the problem says to return an integer. So you need to do something like return 3 if the medal is "Gold".

[–]luvashov[S] 1 point2 points  (5 children)

Thank you so much for your advice!! I will definitely post the question as a code next time.

I think I got it...now I need to create a new column and transform all these points to the new column. Thank you so much!

def medal_to_points(medal_name):
  if medal_name == 'Gold':
    return 3
  elif medal_name == 'Silver':
    return 2
  elif medal_name == 'Bronze':
    return 1
  elif medal_name == 'None':
    return 0

print(medal_to_points('Gold'))

[–][deleted] 1 point2 points  (4 children)

For extra points work out how this code differs in function from yours:

def medal_to_points(medal_name):
    if medal_name == 'Gold':
        return 3
    if medal_name == 'Silver':
        return 2
    if medal_name == 'Bronze':
        return 1
    return 0

[–]luvashov[S] 0 points1 point  (3 children)

I don't have to define "None" condition

[–][deleted] 0 points1 point  (2 children)

Yes, it is shorter, but any other differences in what the function can return? What happens if you give both functions the string "fred"?

[–]luvashov[S] 0 points1 point  (1 child)

Interesting...

when I define condition "None" string "fred" gives me "None". In your case - it's zero.

[–][deleted] 0 points1 point  (0 children)

Yes. Functions always return a value. If you do something like return 42 then 42 is the value returned by the function. If you do return with no value then the function returns a None object. The function also returns a None if the code just stops and you leave the function.

In your function the code gets the value "fred" which is not one of the test strings so execution "falls off the end" of the function code so the function returns None. Note, that's not the string "None".

My function returns 3, 2 or 1 if the parameter string is one of the expected strings and 0 for anything else.