Day 050 - Project 50 : Idea Storage System

If you have any questions, comments or issues with this project please post them here!

1 Like

My code isn’t working :

import random, os, time
while True:
  print('🌟Idea Storage🌟\n')
  print('Operations to be performed:\n1. Add an idea\n2. Load a random idea\n')
  op = input('Choice > ')
  if op == '1':
    f = open('my.ideas', 'a+')
    idea = input('Input an idea > ')
    f.write(f'{idea}\n')
    time.sleep(1)
    print('Your idea has been stored')
    f.close()
    time.sleep(1)
    os.system('clear')
    continue
  else:
    f.open('my.ideas', 'a+')
    r = f.read()
    print(f)
    re = f.read().split('\n')
    f.close()
    print(random.choice(re))
    time.sleep(3)
    continue

Hey, please don’t the same question in multiple places, this just adds clutter to the community space. If your original question doesn’t get answered for a while (close to a week), it would be appropriate to reply to your original post asking for help, which bumps it back to the top of the latest list. If you want to add additional information to your question, you can edit it or add a reply containing this information.

Hi @SUMAYDOHARE thanks for your question. Can you elaborate on this please? What error messages do you see?

If you could also share a link to your Repl it would allow others in the community to take a look at the code, run it, and then offer some ideas.

1 Like
import random, os, time
while True:
  print('🌟Idea Storage🌟\n')
  print('Operations to be performed:\n1. Add an idea\n2. Load a random idea\n')
  op = input('Choice > ')
  if op == '1':
    f = open('my.ideas', 'a+')
    idea = input('Input an idea > ')
    f.write(f'{idea}\n')
    time.sleep(1)
    print('Your idea has been stored')
    f.close()
    time.sleep(1)
    os.system('clear')
    continue
  else:
    if (not os.path.exists("my.ideas")):
      print("you have to add ideas first!")
      continue
    f = open('my.ideas')
    re = f.read().strip().split('\n')
    f.close()
    print(random.choice(re))
    time.sleep(3)
    continue

Can someone explain to me why I need to
f.close()
the file again, after reading out the contents?

to me this feels like like only being able to paste something you have copied from a file after you close the file. A bit strange.

Thanks for the explanation!

The continue before the else is useless.

As for your question. You do not need to open and close all the time, you can also open and close only when you are finished.
To do that you need to add a thirds option to add and read. A stop option.

With that you can open the file before the wile loop and close it once … but then i would advice to Google the with structure for using files. Much cleaner .

1 Like

Thanks for your help

Is someone able to explain why the .split command is used to make the random choice? I’m just confused about that part. I was pretty happy/confident with the add subroutine I created for it - that worked tickety boo. But I couldn’t figure out the random read. I was doing something real convoluted that I couldn’t complete - basically trying to make it a list and figure a way to add a second value for each row that contained a number, that I would then call at random and print the row that went with it. Took a peek at the solution video, and saw it was quite a simple process. But I would really like to understand the logic behind it.

Specifically I am talking about these lines of code:

f = open("my.files","r") # I know what this does - I had it in my code
ideas = f.read().split("\n") # This line of code to me I don't understand the reason for
f.close() # I know what this does as well
ideas.remove("") # David explains this one well also - I understand why it is necessary
idea = random.choice(ideas) # This line of code I am also not understanding the why.  I don't remember a lesson using random.choice, or at least I only vaguely remember something as I type this

Hi @Duke1017

The .split command actually takes the contents of my.files and breaks it into individual chunks every time it encounters the \n character (or new line). Ideas is now a list / 1D array of individual items.

Random.choice selects an item form the ideas list … at random …

Hope this helps!

5 Likes

OK, so I guess I was misunderstanding the split command. Rather than splitting at each individual space in the line of text, by adding the \n modifer in that line of code, it is telling the script to split and start a new line in the list whenever it finds a new line (\n)?

Just want to make sure I’m seeing it right, but I think I understand what it is you are saying now. I’m going to go back and re-watch the modifier lesson (and also try to find that random.choice one to add to my notes I’ve been taking).

Thanks for the quick reply :slight_smile:

3 Likes

is there a reason we shouldn’t export the file contents into a list? would that take up more ram? or is my way extremely inefficient?

import os, time, random

ideas = []

f = open("my.ideas.txt", "r")

while True:
  contents = f.readline().strip()
  if contents == "":
    break
  ideas.append(contents)
f.close()

while True:
  os.system("clear")
  print(f"🌟Idea Storage🌟\n")
  menu = input(f"""1.  Add an idea
2.  Load a random idea
>  """)
  if menu == "1":
    idea = input("New idea > ").strip().capitalize()
    f = open("my.ideas.txt", "a+")
    f.write(f"{idea}\n")
    f.close()
    print(f"Added to file")
    ideas.append(idea)
    print(f"Added to list\n")
    time.sleep(1)
  elif menu == "2":
    random_item = random.choice(ideas)
    print(random_item)
    time.sleep(3)
  else:
    print(f"Menu option does not exist")
    time.sleep(2)

No, not really. Even if your solution is not the same as the given one, if it works, it’s usually fine.

2 Likes

I did it this way but I used it to check if the list was empty, if so then print something to that effect rather than a blank line.