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.
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.