picking a file and reading the words from it python
<p>I need help with this, I'm a total beginner at python. my assignment is to create a program that has the user pick a category, then scramble words from a file that are in that category. I just want to figure out why this first part isn't working, the first part being the first of four different methods that run depending on which category the user picks. </p>

<pre><code>print ("Instructions: Enter your chosen category, animals, places, names or colors.")
viewYourFile = input("Enter your category")

category = 'animals'

if category == 'animals':
    animals = open('animals.txt')
    next = animals.read(1)
    while next != "":
        animal1 = animals.read(1)
        animal2 = animals.read(2)
        animal3 = animals.read(3)
        animal4 = animals.read(4)
        animal5 = animals.read(5)
    animalList = ['animal1', 'animal2', 'animal3', 'animal4', 'animal5']
    chosenAnimal = random.choice(animalList)
    animalLetters = list(chosenAnimal)
    random.shuffle(animalLetters)
    scrambledAnimal = ' '.join(animalLetters)
    print(scrambledAnimal)
    print("Enter the correct spelling of the word")
</code></pre>

<p>The first problem is that you're reading only 1-5 letters from the file.
Please read the (documentation)[<a href="https://docs.python.org/2/tutorial/inputoutput.html]" rel="nofollow">https://docs.python.org/2/tutorial/inputoutput.html]</a> on how the <strong>read</strong> function works.  The number in parentheses is how many bytes you want to read.</p>

<p>You may want a simpler solution, such as reading the entire file and splitting it into words.  This would look something like:</p>

<pre><code>file_contents = animals.read()
animalList = file_contents.split()
</code></pre>

<p>If <strong>split</strong> is new to you, then (look up)[<a href="https://docs.python.org/2/library/string.html]" rel="nofollow">https://docs.python.org/2/library/string.html]</a> that method as well.</p>

<p>The next problem is that you've set your animal list to literal strings, rather than the input values you read.  I think you want the line to read:</p>

<pre><code>animalList = [animal1, animal2, animal3, animal4, animal5]
</code></pre>

python