Python-need help solving this. | Sololearn: Learn to code for FREE!

0

Python-need help solving this.

Create a list clothes = ["socks", "shirt", "skirt", "scarf"] and function insert_element() that will: modify list clothes take 2 arguments: new_cloth, index. Then this function will insert a new_cloth element on the specified index position in the clothes list if the index is not specified on function call, add the new_cloth element at the beginning of the clothes list Call this function 3 times: with 2 arguments, where the index is a positive value with 2 arguments, where the index is a negative value just with 1 argument - new_cloth You can use any values for new_cloth arguments.

4/18/2021 5:57:30 PM

Elisa

5 Answers

New Answer

+11

Natalia Staver , before you can get some help from the community, we would like to see your attempt. please put your code in playground and link it here. if you have not done a try by yourself so far, please kindly do so. Thanks for your understanding

+6

Try solve it. We will help you😁 Happy Coding!

+2

Hi! And now we are waiting for you to try to solve this task...

0

hi Natalia Staver below is an example of what the code could look like along with some comments about each line. i also provided a link to the code snippet as well. # function to insert new clothes with 2 parameters: new_cloth, and index with default as 0 def insert_element(new_cloth, index=0): # default clothes list clothes = ["socks", "shirt", "skirt", "scarf"] # if index is less than 0, convert it to 0 to insert at beginning of list. if index < 0: index = 0 # insert new clothing at specified index clothes.insert(index, new_cloth) # return the new list when function is called return clothes # calling new_clothes function and storing returned value to new_clothes variable. you can change pants to another clothing item and the index. new_clothes = insert_element("pants", -10) # print the new clothes list print(new_clothes) i hope it is helpful in what you are attempting to do. https://code.sololearn.com/cq4r0FN6owk3/?ref=app

0

Thank you so much!