Python Forum
why is this not filling my empty list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is this not filling my empty list?
#1
Could someone tell me why this isn't filling my empty list?
# creating empty list to read in file
    responses = []

    file = open("8_Ball_responses.txt" , "r")

    for line in file:
        responses.append((file.readline()).rstrip('\n'))

    file.close()

    print(responses[random.randint(0, len(responses) - 1)])
Reply
#2
It's hard to say without seeing the file. However, file.readline() conflicts with file. The for loop already read the line for you, and put that into the variable line. So just append that:

for line in file:
    responses.append(line.strip())
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
So I pulled out the readline part of the code to read
# creating empty list to read in file
    responses = []

    file = open("8_Ball_responses.txt" , "r")

    for line in file:
        responses.append(line.rstrip('\n'))

    file.close()

    print(responses[random.randint(0, len(responses) - 1)])
It is still leaving an empty list.
Here is a copy of what is in the text file. The path is correct, the text file is in the same directory.

Yes, of course!
Without a doubt, yes.
You can count on it.
For sure!
Ask me later.
I'm not sure.
I can't tell you right now.
I'll tell you after my nap.
No way!
I don't think so.
Without a doubt, no.
The answer is clearly NO.
Reply
#4
It's working find for me. Instead of that last line I would use random.choice:

print(random.choice(responses))
But I didn't change that before running the code and it worked. Given that your code is indented, and there is no import for random, I'm guessing that's a part of a larger program. Is this code in a function?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Yes, it is part of a larger program. Yes, it is part of a function. I can not use random.choice because we have not used that in class.

import random


def main():
    # creating empty list to read in file
    responses = []

    file = open("8_Ball_responses.txt" , "r")

    for line in file:
        responses.append(line.rstrip('\n'))

    file.close()

    print(responses[random.randint(0, len(responses) - 1)])

    print(responses)


main()

It works now. I had left out the calling of the function thinking if I just ran the code snippet it would still work. Thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,209 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  Can anyone spot why this function is returning an empty list? hhydration 2 1,816 Nov-18-2020, 06:16 AM
Last Post: deanhystad
  Why does this function print empty list? hhydration 1 1,501 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  Turtle Graphics Help - Filling each shape in Nate 10 7,124 Nov-27-2018, 08:40 AM
Last Post: Nate
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,732 Sep-12-2018, 12:43 AM
Last Post: TommyMer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020