Python Forum
Dictionary content overwritten by new key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary content overwritten by new key
#1
Hello - I've searched around for an answer to this, but can't find one so hopefully someone will be able to help!
Background: I'm doing a Python 2 course, but it's not assessed so I won't be pretending I did it all by myself!
The task is to create a 'game' using classes, dictionaries, lists, string formatting etc

The first part of my game writes poems, saves the vocabulary into a dictionary and then appends the finished poem into a text document. This all works fine until I try and write a second poem and it overwrites the first one, despite having a completely different key name. I think this must have something to do with going back to use a function from scratch which somehow empties the dictionary, but I don't know how to structure it to stop it doing this!


class Writing(object):
    def __init__(self):
        self.writing = {}

class Poem(Writing):
    def write_poem(self, text_name):
        self.writing[text_name] = [raw_input("What are your favourite animals? > ")] #0
        self.writing[text_name].append(int(raw_input("How many would you like? > "))) #1
# goes on a bit like this so I'll skip forward

poem_name = raw_input("What is your poem about? > ")
MyPoem = Poem()
MyPoem.write_poem(poem_name)
print MyPoem.writing

text_name2 = raw_input("What is your poem about? > ")
MyPoem = Poem()
MyPoem.write_poem(text_name2)
print MyPoem.writing
The raw_input "What is your poem about? >" sets the key name in the dictionary ('writing' defined in the Writing() class), and the further questions add values to the list attached to that key.
On the first command print MyPoem.writing you get e.g.:
{'key': ['fav animal', 15]}
But then after the second command print MyPoem.writing you get e.g.
{'different key': ['new animal', 24]}
instead of:
['different key': ['new animal', 24], 'key': ['fav animal', 15]}

What is happening? Where should I put the dictionary to stop it being written over?

Thanks
Sophie

Hang on - I think I've answered my own question...

Each poem is a separate instance of the class Writing, but I want all the poems to be listed in one dictionary - so I need one instance called Poems = Poem() or Output = Writing() or something? Yes?
Reply
#2
you have instantiated two copies of MyPoem, each with the same name
instanciate once, and you will be ok
example:
MyPoem = Poem()

poem_name = raw_input("What is your poem about? > ")
MyPoem.write_poem(poem_name)
print(MyPoem.writing)

text_name2 = raw_input("What is your poem about? > ")
MyPoem.write_poem(text_name2)
print(MyPoem.writing)
Reply
#3
Thanks! All sorted...
On to the next problem now...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,690 Nov-21-2019, 10:06 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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