Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list - 2 questions:
#1
question number one:
The function gets a list of strings of even length. The function returns a string that contains the list members in the even positions, separated by a comma and a space, as well as the last member with the caption and in front of it.
my try is:
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)
output is:
[]

I dont get it, no matter what I try, I wont get anything other than the []...
I dont need a solution, What I need is a tip, what to change here ( only one thing, ill try to understand alone the rest ).

EDIT: I know this question doesnt include the and thingy, but I first want to see that im getting something instead of [] at output. when ill get it, ill add it with join function, but first of all, i want to fix this code so it will work.
________________________________________________________________________________________________________

second question:
Write a function called shift_left. The function receives a list of length 3 and returns a new list moved one step to the left.
Function definition === def shift_left (my_list):
Examples of running the shift_left function
>>> shift_left ([0, 1, 2])
[1, 2, 0]
>>> shift_left (['monkey', 2.0, 1])
[2.0, 1, 'monkey']

my try:
def shift_left(my_list):
    """
    This function moves the list with 3 numbers one step to the left.
    If it was 0 1 2, then it will be 1, 2, 0.
    :param my_list: The numbers of the list I will choose.
    :type my_list: str, int, float
    :rtype
    :return: the result of the list with one step left.
    """
    my_list = [param1, param2, param3]
    new_list = my_list[::-1]
well, to be honest ,at this one, I dont have the other tries I had, I had in here 3 solutions, only 1 worked, but it wasnt according to how it should be... the one that worked If I wrote a string inside the my_list at the definition at the end, it would have worked ( i cant have it since it was before hours ago ). the other two are long long gone... this one I just left it like that and went on to the next question ( which is the first question up there, I did it opposite here, but it doesnt matter ).
I managed to find a solution for this one online, but it concluded 2 definitions, I dont think it should be part of the solution... if there is any other thing, ill be happy for a tip also, only a small tip so I can continiue.
_____________________________________________________________________________________________________

now, I know im not using BBCODE and i was told twice in my threads not to post without it, but seriously, the link https://python-forum.io/misc.php?action=help&hid=25 doesnt say at all how to use it... all I understood from there, that i need to put python between my code and thats it... the explanation there is not really understandable, i cant post in bbcode because of it... all i can do is the code thingy... if its not allowed, then i guess ill be stuck ( cus you must use bbcode as i understood ).
Reply
#2
(Jul-31-2021, 11:31 AM)ben1122 Wrote: I dont get it, no matter what I try, I wont get anything other than the []...
I dont need a solution, What I need is a tip, what to change here ( only one thing, ill try to understand alone the rest ).

On line 9, you create a variable called my_list to which you assign an empty list. Then, you check its value and then you slice it and return. That list is empty, so of course you return an empty list.

You shouldn't use the same name for that variable as you're using for the function parameter. Choose a different one.
Reply
#3
(Jul-31-2021, 11:36 AM)ndc85430 Wrote:
(Jul-31-2021, 11:31 AM)ben1122 Wrote: I dont get it, no matter what I try, I wont get anything other than the []...
I dont need a solution, What I need is a tip, what to change here ( only one thing, ill try to understand alone the rest ).

On line 9, you create a variable called my_list to which you assign an empty list. Then, you check its value and then you slice it and return. That list is empty, so of course you return an empty list.

You shouldn't use the same name for that variable as you're using for the function parameter. Choose a different one.

hmm, ill try and update.
and for the second question ( not related to you, just adding, cus I found one of the tries I did, which didnt work ):
def shift_left(my_list):
    """
    This function moves the list with 3 numbers one step to the left.
    If it was 0 1 2, then it will be 1, 2, 0.
    :param my_list: The numbers of the list I will choose.
    :type my_list: str, int, float
    :rtype
    :return: the result of the list with one step left.
    """
    my_list = [p1, p2, p3]
    new_list = []
    new_list[0] = my_list[1]
    new_list[1] = my_list[2]
    new_list[2] = my_list[0]
    return new_list

modified_list = shift_left(my_list=[0, 1, 2])
print(modified_list)
just added so ill have one of the tries, but the one that half-worked me, couldnt find it.

but about what you said to the first question, ill try it now.
Reply
#4
(Jul-31-2021, 11:36 AM)ndc85430 Wrote:
(Jul-31-2021, 11:31 AM)ben1122 Wrote: I dont get it, no matter what I try, I wont get anything other than the []...
I dont need a solution, What I need is a tip, what to change here ( only one thing, ill try to understand alone the rest ).

On line 9, you create a variable called my_list to which you assign an empty list. Then, you check its value and then you slice it and return. That list is empty, so of course you return an empty list.

You shouldn't use the same name for that variable as you're using for the function parameter. Choose a different one.

well, I dont think i really understood what you said...
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.
    """
    modified_list = []
    if len(modified_list) % 2 == 0:
        return modified_list[0::2]

new_list = format_list(modified_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
line 13, in <module>
    new_list = format_list(modified_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
TypeError: format_list() got an unexpected keyword argument 'modified_list'

Process finished with exit code 1
got an error.
Reply
#5
if I do it like that:
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.
    """
    modified_list = []
    if len(modified_list) % 2 == 0:
        return modified_list[0::2]

new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
then ill get an output like this [] again.
Reply
#6
Line 10 in that code: why are you trying to overwrite the value passed for my_list? The variables p1, p2 and p3 aren't defined anywhere (are you trying to do pattern matching as you would in other languages?).
Reply
#7
(Jul-31-2021, 11:43 AM)ben1122 Wrote:
new_list = format_list(modified_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)

The name of the parameter to the function is my_list, not modified_list. Since there's only one parameter, you don't need to use keyword arguments really.
Reply
#8
(Jul-31-2021, 11:45 AM)ndc85430 Wrote: Line 10 in that code: why are you trying to overwrite the value passed for my_list? The variables p1, p2 and p3 aren't defined anywhere (are you trying to do pattern matching as you would in other languages?).

hmm to be honest, at first i did instead of the p1 p2 p3 - i did 0 1 2, that way if i put string at format_list at the end, it would work to me i think ( something like that, one of the first tries ).
but its not good because I need to choose my numbers at the format_list, not at the definiton scope.
Reply
#9
(Jul-31-2021, 11:46 AM)ndc85430 Wrote:
(Jul-31-2021, 11:43 AM)ben1122 Wrote:
new_list = format_list(modified_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)

The name of the parameter to the function is my_list, not modified_list. Since there's only one parameter, you don't need to use keyword arguments really.
about the keyword, yea i know, but i learnt like that, so i prefer to stay with it ( it doesnt matter anyway ).
but yea, I tried both and it still doesnt work, look at my second message.
Reply
#10
The code is still wrong, isn't it? You need to be checking the length of my_list, not modified_list (and the same for the slice) Think about what you're doing - modified_list is the one you're using to hold the return value, my_list is the original, so of course you want to do the operations on the original.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,467 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020