When I try to add a note, and view it after, the value that I put in dissapears and it is not shown in the array. When I input view, I want it to show all the notes I have put in.
while 1:
user = input("Do you want to (View/Add) notes: ").lower()
noteList = []
def addNote():
global note
note = input("Add your notes here: ")
noteList.append(note)
print(noteList)
if user == 'view':
print(noteList)
elif user == 'add':
addNote()
state = True
1 Answer 1
It's because you create a new noteList in every loop.
Just move noteList=[] out of your loop and it will be fine.
noteList = []
while 1:
user = input("Do you want to (View/Add) notes: ").lower()
def addNote():
global note
note = input("Add your notes here: ")
noteList.append(note)
print(noteList)
if user == 'view':
print(noteList)
elif user == 'add':
addNote()
state = True
answered Jul 13, 2022 at 5:29
Crazy Engineer
2901 silver badge13 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
noteList = []on every iteration.noteListto an empty array. You probably want to move that initialization outside of the while loop. Also, there's no need to useglobal note.