list - 2 questions: - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list - 2 questions: (/thread-34441.html) |
RE: list - 2 questions: - ben1122 - Jul-31-2021 (Jul-31-2021, 12:05 PM)ndc85430 Wrote: The code is still wrong, isn't it? You need to be checking the length of yea, that is what I did, i checked the len of the my_list, and not modified_list. but you told me in the other comment that i need to replace all my_list in modified_list, so i re-uploaded it as modified_list, but then you told me its not what you meant by that. I cant really understand what you say to me ill try to read again, but I dont think.. Im trying something like that: i did my_list as the name of the func and added [] ( empty list ). then checked lenght of it. uh im confused myself now... i guess I wont be able to complete it... RE: list - 2 questions: - ndc85430 - Jul-31-2021 (Jul-31-2021, 12:08 PM)ben1122 Wrote: but you told me in the other comment that i need to replace all my_list in modified_list Emphasis mine. No I did not. I said you needed to use a different variable name for the modified list, that's all. If it makes life simpler, just get rid of modified_list if you don't want to assign to it.
RE: list - 2 questions: - ben1122 - Jul-31-2021 (Jul-31-2021, 12:10 PM)ndc85430 Wrote:(Jul-31-2021, 12:08 PM)ben1122 Wrote: but you told me in the other comment that i need to replace all my_list in modified_list okay, so I returned to this state: def format_list(my_list): """ This function returns the words in the list which is in the even side. :param my_list: The list of the project. :type my_list: str, int, float :rtype: str, int, float :return: The result of the list on the even only. """ my_list = [] if len(my_list) % 2 == 0: return my_list[0::2] new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]) print(new_list)its the beginning one. I still dont know what is my mistake in here... I removed the thing you told me at start, so its like that now: def format_list(my_list): """ This function returns the words in the list which is in the even side. :param my_list: The list of the project. :type my_list: str, int, float :rtype: str, int, float :return: The result of the list on the even only. """ my_list = [] if len(my_list) % 2 == 0: return my_list[0::2] print(format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]))but still output is the same [], nothing I do changes it. RE: list - 2 questions: - ben1122 - Jul-31-2021 def format_list(my_list): """ This function returns the words in the list which is in the even side. :param my_list: The list of the project. :type my_list: str, int, float :rtype: str, int, float :return: The result of the list on the even only. """ if len(my_list) % 2 == 0: return my_list[0::2] print(format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]))oh wait , I changed something and now its worked. i removed the my_list = [] why did it suddnely work? RE: list - 2 questions: - ben1122 - Jul-31-2021 ['hydrogen', 'lithium', 'boron']its the output, its good i think no? RE: list - 2 questions: - ben1122 - Jul-31-2021 I cant seem to understand, if I removed my_list = [] and its good now, why is it like that? it means that my_list shouldnt be a list, but it is a list now, athough i removed []. any explanation please for it? as I know, in order for a variable to be a list, it has to have [] or something else. but in here it doesnt have it. RE: list - 2 questions: - ndc85430 - Jul-31-2021 Why did you think you needed the line my_list = [] in the first place? All it was doing was creating a local variable called my_list that was shadowing the function parameter, so that the function parameter was inaccessible.
RE: list - 2 questions: - ben1122 - Jul-31-2021 (Jul-31-2021, 12:23 PM)ndc85430 Wrote: Why did you think you needed the line because otherwise it wouldnt count as a list, but I just realized that the new_list thingy, because it has there My_list and i did there [] with stuff in it, it counts as list. that thing is new to me, thanks :) about the last thing of the question, adding the and magnesium. if im doing new_list[-1] ill get: and boron ( of course, because new list is a other list with jumps of 2 ). if ill try my_list, it wont work, because its not a variable. what am i supposed to do now? I tried to create a new variable of list and then modify it to have the last -1 of the my_list, but nothing seems to work. RE: list - 2 questions: - ndc85430 - Jul-31-2021 I don't understand what you're asking. A better way to go about this is to show what input you're giving, what you expect to get out and the code you're trying to achieve that. RE: list - 2 questions: - ben1122 - Jul-31-2021 I tried to do something global and then use it, but for some reason global is not working here... def format_list(my_list): """ This function returns the words in the list which is in the even side. :param my_list: The list of the project. :type my_list: str, int, float :rtype: str, int, float :return: The result of the list on the even only. """ if len(my_list) % 2 == 0: joined_list = my_list[-1] global joined_list return my_list[0::2] new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]) print(new_list) joined_list # Here its not working, the global, it does nothing, why? |