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!
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?
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.writingThe 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?