0

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
asked Jul 13, 2022 at 5:17
3
  • You are running noteList = [] on every iteration. Commented Jul 13, 2022 at 5:20
  • 1
    Every time you start your loop, you reset noteList to an empty array. You probably want to move that initialization outside of the while loop. Also, there's no need to use global note. Commented Jul 13, 2022 at 5:20
  • @KlausD. Tyyy :) Commented Jul 13, 2022 at 5:27

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.