1

What i am trying to do is go through a string, choose a name, and a job title that is currently in string, then it will find the line that the job title is on, than replace the current name there with the new name chosen in the argument.

This is my string:

TV = '''
Tulleveien Velforening
leder: Kari
kasserer: Ole
IT-ansvarlig: Liv
parkeringsansvarlig: Kari
arrangementsansvarlig: Liv
hagekonsulent: Kari
brannansvarlig: Kari
'''

Then this is the function i am trying to run to actually perform it:

def Verv(navn, vervs):
    print("Tulleveien Velforening")
    for lines in TV.split("\n"):
        lines.replace(navn, lines)
        return TV

And this is what i tried:

Verv('Roald', ["leder", "IT-ansvarlig"])

But what i get when i run the program is just the list how it was in the beginning. The expected output would have been:

Tulleveien Velforening
leder: Roald
kasserer: Ole
IT-ansvarlig: Roald
parkeringsansvarlig: Kari
arrangementsansvarlig: Liv
hagekonsulent: Kari
brannansvarlig: Kari

How would i solve this problem?

3 Answers 3

3

Try this, I have added some inline comments which should explain the logic.

TV = '''
Tulleveien Velforening
leder: Kari
kasserer: Ole
IT-ansvarlig: Liv
parkeringsansvarlig: Kari
arrangementsansvarlig: Liv
hagekonsulent: Kari
brannansvarlig: Kari
'''


def Verv(navn, verv):
    # Created an empty dictionary to convert the lines into a dictionary which is much easier to manage.
    rin_dict = {}
    print("Tulleveien Velforening")
    # Forming a dictionary out of your string.
    for line in TV.split("\n"):
        if ":" in line:
            rin = line.split(": ")
            rin_dict[rin[0]] = rin[1]
    # Dictionary key replacement.
    for elm in verv:
        rin_dict[elm] = navn
    # Printing back the dictionary in your desired format.
    for k, v in rin_dict.items():
        print("{0}: {1}".format(k, v))


# The second argument is a list.
Verv('Roald', ["leder", "IT-ansvarlig"])

Sign up to request clarification or add additional context in comments.

Comments

2

Honestly, Verv is very wrong. Here it is step by step:

  1. print("Tulleveien Velforening")
    • Tulleveien Velforening is already in the input string, so why do you need to print it again?
  2. for lines in TV.split("\n"): - Access TV (from the containing scope), split it on newlines, and loop over each element as lines.
    • Ideally TV should be passed in as an argument, but it's not a big deal
    • lines should be renamed line, since it contains a line, not an iterable of lines.
  3. lines.replace(navn, lines) - Replace any occurrences of navn with the entire line itself
    • 'Roald' doesn't occur in the input, so this does nothing
    • Substituting lines doesn't make any sense
    • Probably you wanted something more like line.replace(..., navn)
  4. return TV
    • TV hasn't been changed in the function
    • The return value of Verv is unused

Note that the verv parameter is not used.

Also note there's no output except Tulleveien Velforening.


Here's what you want to do:

(This is one possible approach. Rineesh's answer is another.)

  1. Loop over each line of TV. I would write for line in TV.splitlines():
  2. (optional) If the line is empty, continue to the next loop
  3. Check whether the line starts with any verv in vervs
    1. If yes, replace the second field in line with navn
  4. Print the lines

I'll leave the implementation up to you.

Comments

-2

You should replace lines.replace(navn, lines) with lines=lines.replace(navn, lines). replace returns a copy of the original string with the relevant substrings replaced .

1 Comment

lines is a local variable and a loop variable at that, so this will have no effect. There's a deeper problem but personally I'm not quite sure what.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.