Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list - 2 questions:
#21
(Jul-31-2021, 12:37 PM)ndc85430 Wrote: 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.

I need to get :

hydrogen, lithium, boron, and magnesium

that is my final answer.
the first 3 is good, the fourth magnesium, i need to get it from the original list, do -1 to the list to get it and add - and magnesium ( the -1 of list ).
I tried to do up there, but it wont work. the global is not working correctly for some reason.
Reply
#22
edit: oh im an idiot, one minute
Reply
#23
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:
        global joined_list
        joined_list = my_list[-1]
        return my_list[0::2]

new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
final_list = f"{new_list} and {joined_list}"
print(final_list)
['hydrogen', 'lithium', 'boron']
['hydrogen', 'lithium', 'boron'] and magnesium
what is happening here? why dont i get it inside the list?

edit:
managed to almsot get it, but theand is problematic.

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:
        global joined_list
        joined_list = [my_list[-1]]
        return my_list[0::2]

new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
final_list = f"{new_list} and {joined_list}"
print(final_list)
['hydrogen', 'lithium', 'boron']
['hydrogen', 'lithium', 'boron'] and ['magnesium']
how do i add and?
Reply
#24
Why are you using a global in the first place? Shouldn't your function just return the right list?
Reply
#25
(Jul-31-2021, 12:50 PM)ndc85430 Wrote: Why are you using a global in the first place? Shouldn't your function just return the right list?
It should, but I dont know how to do it correctly...
Im trying to do it in this topic, but I cant seem to understand how to... I know its not the good solution, but as said, I dunno how to do it.. no matter what I do or change, nothing helps, all it does is destroy what i did or nothing...
Reply
#26
Right. If you don't know what code to write, then stop. Can you solve the problem with pen and paper? Can you express the steps in solving the problem in words? Start there - code comes from that.
Reply
#27
(Jul-31-2021, 01:00 PM)ndc85430 Wrote: Right. If you don't know what code to write, then stop. Can you solve the problem with pen and paper? Can you express the steps in solving the problem in words? Start there - code comes from that.

I dont know how to..
as you see I almost finished the question, i pretty understood in the meantime what i did.
but i cant seem to understand how to add the and to the list.
about the pen and paper - no idea also...
Reply
#28
If you had no computer and had to explain how to take a list and produce a new list containing the output you wanted, what would you say?
ben1122 likes this post
Reply
#29
(Jul-31-2021, 01:24 PM)ndc85430 Wrote: If you had no computer and had to explain how to take a list and produce a new list containing the output you wanted, what would you say?

to be honest, i have no idea...
i guess ill just give up on lists.
thanks anyway, i already moved on also to the next thing to be honest, i saw i didnt mange to answer it and after 4hours, i realized i will never will.

if a admin sees this, you can close it, thanks :)
Reply
#30
Unfortunately, if you're unable to break down and solve problems in a structured way, I think you're going to find programming quite frustrating.

In any case, good luck!
ben1122 likes this post
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 38,709 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