#🔒 Help with class homework (basics)

26 messages · Page 1 of 1 (latest)

runic quartz
#

After reading in a file with 4 attributes, all comma seperated, I have trouble with making it into an object.
Code is:


class Aminosyra():
    def __init__(self, kod, namn, grupp, molekylvikt):
        self.kod = kod
        self.namn = namn
        self.grupp = grupp
        self.molekylvikt = molekylvikt

    #def __str__(self, kod, namn, grupp, molekylvikt:
 

def inläsning_fil_till_lista_objekt(inläst_fil):
    objekt_lista = []
    with open(inläst_fil, "r", encoding = "utf-8") as fil:
        for rad in fil:
            lista_attribut = []
            for attribut in rad.split(","):
                if attribut:
                    attribut = attribut.strip()
                    lista_attribut.append(attribut)         objekt_lista.append(lista_attribut)

    return objekt_lista

##kod = attribut[0]
##namn = attribut[1]
##grupp = attribut[2]
##molekylvikt = attribut[3]
##objekt = Aminosyra(kod, namn, grupp, molekylvikt)

worn ospreyBOT
#

@runic quartz

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

runic quartz
#

the error that comes up is an index error after index 0

#

What I expect to happen is that after splitting each line and makes a list out of it, I use the class to make a list, like how I've commented out near the end, then I would append that to the bigger list so I have an object list with all objects in it

ivory yacht
#

It would help if you write things in english.

#

Are you reading from a txt?

runic quartz
#

yes, want me to send the file? its short

runic quartz
ivory yacht
ivory yacht
quartz olive
#

problem is the commented lines belong at the same level of indentation as "for attribut in rad.split" but below the for loop

#

he was accessing the list before it was populated

runic quartz
#

yeah gimme a moment

quartz olive
#

just informing ACE lol

runic quartz
#

ah ok

#

just wanted to inform why I didnt reply to you

#

code runt but I needed to adda repr method to the class, but now that has an issue

quartz olive
#

is it the missing ) before the :

runic quartz
#

no thats the str I commented out

#

I added a repr

#

ok I got the help I needed, can I close this channel or do the mods or someone need to do that?

ivory yacht
thorny ginkgo
#

The fact that this is in Swedish makes things a little tricky, but that is not a big problem. What you have not said is what you are trying to do. Neither have you given any sample data or expected output. So all you have really said is "my code does not work". What I do see is a class that is not instantiated and a function that is not called.

worn ospreyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.