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