1

I’m doing a school assignment where we have to make a Tamagotchi. Now I have a problem with the text file where the food for the Tamagotchi is saved. I have to order that file on alphabetic order and on the length of the word. Like this.

This is how the words should display (the words are in Dutch). The g and o are later added to the file and have to stay behind the word’s.

The program also adds new food to the list and then it has to go in the right order again. This is what I already made.

def voeding_toevoegen():
file = open('voeding.txt', 'a')
toe_te_voegen_voeding = (str(input('voer voeding in (bijvoorbeeld wortel, snickers, spitskool, etc.)\n')))
file.write(toe_te_voegen_voeding)
g_of_o(file, toe_te_voegen_voeding)

def g_of_o(file, toe_te_voegen_voeding):
gezond_ongezond = (str(input('is deze voeding gezond (g) of ongezond (o)?\n')))
if gezond_ongezond == 'g':
    file.write(':g\n')
    print('de voeding "'+ toe_te_voegen_voeding +'" is toegevoegd aan de lijst')
elif gezond_ongezond == 'o':
    file.write(':o\n')
    print('de voeding "'+ toe_te_voegen_voeding +'" is toegevoegd aan de lijst')
else:
    print('geen geldigen optie (kies een "g" of een "o"')
    g_of_o(file, toe_te_voegen_voeding)

file.close()


voeding_toevoegen()

Then it goes to the second function where it ask’s you if the food you just put in is healthy (g) or unhealthy (o). this goes right behind the food in the file. If you don’t choose g or o the computer say’s its wrong and ask’s the question again. Until you put it right. Then it say’s print ( the food …. Is added to the file).

I hope you can understand the program. Now the question is how do I get the file in a specific order of length and alphabetic?

I hope you can help me.

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
lotte
  • 19
  • 1
  • 1
    I would say you will need to read the file until you find the place where the aliment must go. Python knows how to compare strings according to alphabetical / lexical order (`"a"<"b"` returns `True`, for example). – Daneel Dec 01 '16 at 10:21
  • Sounds like [this](http://stackoverflow.com/questions/4659524/how-to-sort-by-length-of-string-followed-by-alphabetical-order) could answer your question? You can read the content of the file to a string, sort it however you want, and then save it back to the file if needed. – nostradamus Dec 01 '16 at 10:26
  • You have at least two options: 1) read the complete file to memory, add a line, sort it, and write it back (as implicitly suggested by nostradamus). You can also try to insert your new line directly into the file at the right place. You cannot do this with the file.write method, as it overwrites instead of inserting. The fileinput module might help you with that. It is explained e.g. here http://stackoverflow.com/questions/1325905/inserting-line-at-specified-position-of-a-text-file and here http://stackoverflow.com/questions/15147836/search-text-file-and-insert-line – Frank Lauterwald Dec 01 '16 at 10:32

0 Answers0