Skip to content

Instantly share code, notes, and snippets.

@amitkrishna
Created July 20, 2020 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitkrishna/86f8a4e266bf8b8c34c956fc868fa257 to your computer and use it in GitHub Desktop.
Save amitkrishna/86f8a4e266bf8b8c34c956fc868fa257 to your computer and use it in GitHub Desktop.
Exercism 2Fer
def two_fer(name = ''):# siting default argument as ''
#name!=''?print('One for '+name+',one for me'):print('One for you , one for me');
if name!='':
#print('One for',name,', one for me')
# print(f"One for {name}, one for me")
f"One for {name}, one for me"
else:
f"One for you, one for me"
#print('One for you , one for me')
@Fytex
Copy link

Fytex commented Jul 21, 2020

The purpose of this exercise was to return a string so you need to use the keyword return followed by the string. Ex.: return f"One for {name}, one for me".

The difference between strings and f-strings is that f-strings change the followed pattern {variable} by the respective variable. What I want to say is that there was no need to use f"One for you, one for me" since you aren't using any variable. You could simply use a string.

In my opinion the best approach to this exercise would be:

def two_fer(name = 'you'):
    return f'One for {name}, one for me'

But pay attention that in this case if you call the function passing '' (empty string) as argument it will return 'One for , one for me'. However, your code will return 'One for you, one for me'. This happens because an empty string is an object so it doesn't trigger the default argument. This is ambiguous and depends on what the programmer wants.

One last thing. Please don't mix both "" and ''. Stick with one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment