Https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-function-arguments/exercises/positional-argument-unpacking

https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-function-arguments/exercises/positional-argument-unpacking

Hi there

I have trouble understanding why this code:

from os.path import join

path_segment_1 = "/Home/User"

path_segment_2 = "Codecademy/videos"

path_segment_3 = "cat_videos/surprised_cat.mp4"



print(join(path_segment_1, path_segment_2, path_segment_3))

def myjoin(*strings):

  sentence = []

  for string in strings:

    sentence += string

  return sentence

    

print(myjoin(path_segment_1, path_segment_2, path_segment_3))

has a totally different result from this:

from os.path import join

path_segment_1 = "/Home/User"

path_segment_2 = "Codecademy/videos"

path_segment_3 = "cat_videos/surprised_cat.mp4"



print(join(path_segment_1, path_segment_2, path_segment_3))

def myjoin(*args):

  joined_string = args[0]

  for arg in args[1:]:

    joined_string += '/' + arg

  return joined_string

print(myjoin(path_segment_1, path_segment_2, path_segment_3))

Can anyone help? Why, different from the example shown, should we not start with an empty list but with args[0]?
And another question: how do I fit my code in one of those nice blue boxes, so it’s easier to see the difference between the message and the code?

Thanks!

here:

sentence += string

+= for a list is list extending, an example of list extending done right:

print([1, 2, 3] + [4, 5, 6]) # will print: [1, 2, 3, 4, 5, 6]

to append items to a list, use .append(). Or use + for string concatenation, that is also possible.

both approaches work, I would start with an empty list. This:

joined_string = args[0]

assumes there is at least one element, if there isn’t, you will get an error.

you need to apply format/markup, see the third point of this topic:

[How-to] Create a topic that everyone will read

1 Like

Hi (again) stetim94,

first of all: thanks for all the help recently.

As for the code: my code works but prints this:

[’/’, ‘H’, ‘o’, ‘m’, ‘e’, ‘/’, ‘U’, ‘s’, ‘e’, ‘r’, ‘C’, ‘o’, ‘d’, ‘e’, ‘c’, ‘a’, ‘d’, ‘e’, ‘m’, ‘y’, ‘/’, ‘v’, ‘i’, ‘d’, ‘e’, ‘o’, ‘s’, ‘c’, ‘a’, ‘t’, ‘’, ‘v’, ‘i’, ‘d’, ‘e’, ‘o’, ‘s’, ‘/’, ‘s’, ‘u’, ‘r’, ‘p’, ‘r’, ‘i’, ‘s’, ‘e’, ‘d’, '’, ‘c’, ‘a’, ‘t’, ‘.’, ‘m’, ‘p’, ‘4’]

but I would expect the same output as with the ‘code solution’.

I think, for these kind of assigments the question is more: how are students supposed to know how to do this, when it’s not explained in the examples?

but you are using list extending while you have a list and a string

You could check documentation on how to add elements to a list (or use a search engine to find this information for you), or come to a forum or Q&A site. There are several options

I know, but I am especially referring to this bit:

def myjoin(*args):

joined_string = args[0]

for arg in args[1:]:

joined_string += '/' + arg

return joined_string

what is meant by args[0] and args[1:], and what do they actually take in and mean, in this particular case?

well, you could add print() to see what args[0] and args[1:] gives. to help you figured out what might be happening.

1 Like

Yes, I will do that. Cheers!

Hmmm, that didn’t help.

this code:


def myjoin(*args):

  joined_string = args[0]

  for arg in args[1:]:

    joined_string += '/' + arg

  return joined_string

    

print(args[0])

print(args[1:])

returned this error:

Traceback (most recent call last):
  File "script.py", line 23, in <module>
    print(args[0])
NameError: name 'args' is not defined`

It would appear that those print statements are outside of the function, since they come after a return. It would help if you formatted the code correctly.

thanks codeneutrino! I put them inside the function. Works.

By the way, good you brought the subject up, and maybe I’m the only student, but sometimes it’s really hard to know which variables can and can not be used outside a function. Of course, I don’t have the examples ready, but I’m having trouble with this some times.

So basically, after a return statement, arguments cannot be used anymore?

Thanks a lot for your patience and help!

A return statement ends the function. Arguments can only (really) be used inside a function, so yes, you can’t use arguments after a return in the same logic block. This is allowed:

def some_func(param):
  if param == condition:
    return True
  else:
    param += 5
  return param

Since you’re using the param in a different block. Keep in mind:

def some_func(param):
  for i in range(1):
   return True

The function ends inside the for loop, meaning all code after the return won’t be executed.

OK, that makes a lot of sense. In the course of time, sometimes I tend to forget basic things that I learned way, way back.

Have a nice day!

1 Like

At certain points, certain concepts (like variable scope) should sink in. There is no shame in going back and refresh these concepts until you know them through and through.

1 Like