Question: I just started programming with python 2.7, and I have some trouble with my random generator for taking items from an outside .txt file, wondering if anyone could help me a little bit...

Basically Id like to take 100 random hits, and count them all up in the end, and print the summary for the strings. I hope it makes sense!

I know this question has been answered before, but the answeres havent helped me.

So, my txt file contains a bunch of strings, separated with commas.
This is what my code looks like:

```
import random

fname = raw_input("Enter filename: ")
infile = open(fname,'r')
data = infile.readlines()

for i in range(100):
   print(random.uniform("Car_types.txt"))

```

Thanks for your help in advance!

Answer: 
This is probably what you're looking for:

```
import random

fname = raw_input("Enter filename: ")
# With statements are better as they explicitly close the file
with open(fname,'r') as infile:
    data = infile.read().split(",")  # Split the strings with commas as you said

for i in range(100):
   print(random.choice(data))  # This will randomly choose one of the strings

```
I just used a text file to test my code. The test file looks like this(not sure if your file is in the same format as mine).

```
import random
def addElements(l):
    for i in l:
        global result
        result.append(i)
fname=raw_input("Enter filename: ")
result=[]
summary=[]
for line in open(fname,'r'):
    elements=line.strip().split(',')
    addElements(elements)
for i in range(100):
    summary.append(random.choice(result))
print summary

```

1. test.txt:

abc,def,ghi

jkl,mno,pqr

2. Results:

['mno', 'ghi', 'pqr', 'jkl', 'abc', 'abc', 'jkl', 'jkl', 'jkl', 'def', 'pqr', 'def', 'jkl', 'pqr', 'mno', 'jkl', 'mno', 'def', 'jkl', 'jkl', 'def', 'mno', 'pqr', 'pqr', 'abc', 'pqr', 'def', 'abc', 'pqr', 'def', 'jkl', 'pqr', 'mno', 'mno', 'mno', 'def', 'jkl', 'def', 'ghi', 'jkl', 'pqr', 'mno', 'ghi', 'mno', 'mno', 'jkl', 'jkl', 'ghi', 'abc', 'ghi', 'ghi', 'jkl', 'mno', 'def', 'ghi', 'ghi', 'abc', 'abc', 'pqr', 'ghi', 'abc', 'abc', 'mno', 'def', 'jkl', 'mno', 'mno', 'jkl', 'jkl', 'pqr', 'pqr', 'pqr', 'jkl', 'def', 'jkl', 'mno', 'mno', 'ghi', 'jkl', 'mno', 'ghi', 'pqr', 'abc', 'ghi', 'def', 'ghi', 'ghi', 'jkl', 'def', 'abc', 'abc', 'ghi', 'jkl', 'pqr', 'pqr', 'pqr', 'def', 'pqr', 'pqr', 'def']

Hope this helps you out. 

If it still prints out nonsense for you, please provide me how your txt file looks like. Thanks!