0

My problem is my list(lst) is not storing in the file. WONDERFUL COMMUNITY YOU HAVE. I am new to the site so please be aware of that in the comments. The following is my code I was able to store it long ago but there were problems so I undid it and for some reason, in my head, I CANNOT REMEMBER what I did to store it!

days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
    userInput = input("What day of the week would you like to assign a event: ")
    lst = []
    input_words=userInput.lower().split()
    for word in input_words:
        if word in days:
            lst.append(word)
            print(word)
            # set answered to True boolean
            answered = True 
            print((lst))      
    # only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
    if answered == False:
         print("You typed days of week wrong! Try Again!")
def listToString(): 
  if 0 < 2:
    test = open('hold.txt', 'a')
    print(test.write(lst + '\n'))
    test.close()
    return listToString

'a+' does not work!! in repl.it I have tried many things that would be a waste to mention just really trust me I have a stroke explaining. But when I do put lst into the file i want it to be spaced out on each line so for each item i would like it on a new line but for not just storing 1 item. I am new to this site. Also does any one wanna tell me why my code keeps giving me the output of what appears to be the size of 1 item in a list that I write to my file

quamrana
  • 37,849
  • 12
  • 53
  • 71
jeffy
  • 31
  • 7
  • 3
    Perhaps you forgot to call `listToString()`? – quamrana May 10 '21 at 13:11
  • Hey it worked!!! Also I removed the ''\n'' in the function and also did (str(lst) in the function but after the normal stuff is done printing while it does store it gives me some output saying 10 any idea what that could be? – jeffy May 10 '21 at 13:19
  • 1
    The output of `10` sounds like the number of bytes written to the file. – quamrana May 10 '21 at 13:19
  • The code im writing in repl.it is 5KB also in the file im writing too is an empty file with just ['monday'] but it seems you could be write cause when I do tuesday to the file it increases by 1 but if i go back to monday it is 10! so im guessing its just printing the size of 1 item? – jeffy May 10 '21 at 13:25
  • 1
    In your code: `test.write(...)` returns the number of bytes written to the file. Your code then prints that number. – quamrana May 10 '21 at 13:26
  • My python teacher failed me, I failed myself also. – jeffy May 10 '21 at 13:52
  • Please don't wholesale change your question. This needs to be left alone for future readers. If you have further questions you should start a new question. – quamrana May 10 '21 at 15:33
  • Ah, I see. I apoligize. – jeffy May 10 '21 at 15:44

4 Answers4

3

Below points need changes:

  • Need to convert list to str
  • Call listToString() method
days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
    userInput = input("What day of the week would you like to assign a event: ")
    lst = []
    input_words=userInput.lower().split()
    for word in input_words:
        if word in days:
            lst.append(word)
            print(word)
            # set answered to True boolean
            answered = True 
            print((lst))      
    # only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
    if answered == False:
         print("You typed days of week wrong! Try Again!")
def listToString(): 
  if 0 < 2:
    test = open('hold.txt', 'a')
    # and list is not able to con-cat with str, so need to convert that too
    test.write(str(lst) + '\n')
    test.close()
    return listToString

# you need to call the method to execute the method code
listToString()
Akshay Jain
  • 790
  • 1
  • 7
  • 21
  • Hey it worked!!! Also I removed the ''\n'' in the function and also did (str(lst) in the function but after the normal stuff is done printing while it does store it gives me some output saying 10 any idea what that could be? – jeffy May 10 '21 at 13:19
  • 1
    `print(test.write(str(lst) + '\n'))` because of this, change this to `test.write(str(lst) + '\n')` – Akshay Jain May 10 '21 at 13:24
  • So your saying just the print statement messes me up I mean I did what you said but I don't understand how a print would make a difference. – jeffy May 10 '21 at 13:53
  • if you write in the print statement then it will just print the length of it – Akshay Jain May 11 '21 at 03:13
1

Did you mean to call your function:

def listToString(lst): 
    with open('hold.txt', 'a') as test:
        test.write(str(lst) + '\n')

listToString(lst)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Yes thank you I did forget that i'm discussing a new problem that came along with this with some people who answered my question. I will just copy and paste my new problem and what I said: Hey it worked!!! Also I removed the ''\n'' in the function and also did (str(lst) in the function but after the normal stuff is done printing while it does store it gives me some output saying 10 any idea what that could be? – jeffy May 10 '21 at 13:22
1
days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
    userInput = input("What day of the week would you like to assign a event: ")
    lst = []
    input_words=userInput.lower().split()
    for word in input_words:
        if word in days:
            lst.append(word)
            print(word)
            # set answered to True boolean
            answered = True 
            print(lst)      
    # only evaluated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
    if answered == False:
         print("You typed days of week wrong! Try Again!")

def listToString(): 
    with open('hold.txt', 'w') as f:
    for day in lst: 
        f.write(day + '\n')

listToString()

It is also advised to use with to open and close your files. You can find more information about it here. I printed every day entry on a separate line, if you want to just print your list on one line you can change this back and add str(lst).

William
  • 352
  • 4
  • 16
  • Yes thank you I did forget that i'm discussing a new problem that came along with this with some people who answered my question. I will just copy and paste my new problem and what I said: Hey it worked!!! Also I removed the ''\n'' in the function and also did (str(lst) in the function but after the normal stuff is done printing while it does store it gives me some output saying 10 any idea what that could be? – jeffy just now Edit Delete – jeffy May 10 '21 at 13:27
  • Have you tried opening your file in `'w'` mode instead of `'a'`, as I did in my example? I could see the words in my file when I tested it. – William May 10 '21 at 13:42
  • ``` def listToString(): if 0 < 2: test = open('hold.txt', 'a') test.writelines(line+'\n' for line in lst) test.close() return listToString() ``` tis is my code now and it works – jeffy May 10 '21 at 15:22
1

I checked your code. Just so you know what quamrana said is necessary, I ran the code and think there's two things you might be failing on:

def listToString(): 
  if 0 < 2:
    test = open('hold.txt', 'a')
    test.writelines(line+'\n' for line in lst)
    test.close()
    return

if __name__=="__main__":
  days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
  answered = False
  while answered == False:
      userInput = input("What day of the week would you like to assign a event: ")
      lst = []
      input_words=userInput.lower().split()
      for word in input_words:
          if word in days:
              lst.append(word)
              print(word)
              # set answered to True boolean
              answered = True 
              print((lst))
      # only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
      if answered == False:
          print("You typed days of week wrong! Try Again!")

  1. If you call the function just as it is, then it will give you an error since lst is a list and '\n' is a string, therefore, what i propose is using the writelines method that writes every element on the list and adds a '\n' at the end of it
  2. I think, you might want to pass lst as a parameter and not using a global variable Note: I formatted the file, for it to be easier to read
Y34x4t3
  • 19
  • 7
  • I suppose I could use this I just don't know exactly whats going on anything added I would like to be able to understand and grow in it. Would you mind explaining to a noobie or leave a link. Thank you for the help regardless. – jeffy May 10 '21 at 13:35
  • Also the code you gave me is not storing in hold.txt. – jeffy May 10 '21 at 13:36
  • false alarm it works im too ashamed to say the misttake i just made again – jeffy May 10 '21 at 13:38
  • Didn't call the function intentionally, but don't worry, It happens like a lot – Y34x4t3 May 12 '21 at 21:13
  • Can you explain why you added the __name__ i printed it just calling it __name__ and it obviously just had output __main__ so whats the point I though you were making it a home screen that I could call on but its not so mind explaining? – jeffy May 13 '21 at 13:02
  • The name is __main__ if you’re running the file itself, its a good practice to add this line since it helps you to understand that’s only gonna happen if you run the file itself. The rest of the functions are outside that if statement and because of that, you can import that functions in other module, so, you could have another file with maybe names and just call the function listToString from this file and avoid rewriting the code from that function. – Y34x4t3 May 13 '21 at 14:19