Day 50 - Idea Storage - My Code is not 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

Welcome to the community! :smile:

A Repl link is often better than just pasting your code, but pointing out specific problem code is always good. I believe this Repl is the Repl in question, and the code has changed a bit since you posted this question (just half an hour ago), which is a good example of why you might want to at least include the link to the cover page of your Repl.

It’s also a good idea to be clear and provide as much detail as possible about the issue you’re having, ā€˜my code is not working’ could mean a lot of things… Is there an error (in this case running doesn’t result in one)? Is the code silently failing? Is the code not giving the output you expect?

From what I can see, in your else statement you want to load and print a random value from your my.ideas file (presumably a list separated by newlines). What you are doing with this code: open('my.ideas', 'a+') is opening the file in append and read mode, you probably just want read mode: open('my.ideas', 'r'), but this shouldn’t cause any issues… (also I’ve noticed you made this change in your Repl).

When running your Repl, everything seems work fine, did you resolve this issue yourself?

Thank you for responding so soon. I’m sorry that I didn’t provide much information. The issue was that when I wanted to print a random idea from the file, it just printed blank lines. I changed the code, then it gave an ā€œUTF-8ā€ and ā€œunicode encodingā€ something like that. At the end I got frustrated and deleted all my code and studied the solution.
I should’ve provided the link of the repl. Sorry… I also should’ve provided more information on the issue. One again, thank you for your help.