Add an element to a liste - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Add an element to a liste (/thread-25789.html) |
Add an element to a liste - cyril31 - Apr-11-2020 Hello, I must do the following task: Write a recursive function ajouter(element, set) which returns a set (represented by a list) to which a new element has been added (if it does not already belong to the set). Do not use the keyword "in". I already started this program but I have a problem for when I have to return the function: I lose the starting set. def ajouter(element, ensemble): if ensemble==[]: ensemble.append(element) return ensemble else: if ensemble[0]==element: return ensemble else: return ajouter(element, ensemble[1:]) element=4 ensemble=[1,2,3]Can you help me please ? Thank you for the answer Cyril from France RE: Add an element to a liste - pyzyx3qwerty - Apr-12-2020 Shouldn't you be calling the function, like: print(ajouter(element, ensemble))Next, how about you store the 'ensemble.append(element)' as another thing? Last,(I'm not so sure)i don't think so that you should use more than 1 returns in your code |