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.
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)
scrambledAnimal = ' '.join(animalLetters)
print("Enter the correct spelling of the word")
The first problem is that you're reading only 1-5 letters from the file. Please read the (documentation)[https://docs.python.org/2/tutorial/inputoutput.html] on how the read function works. The number in parentheses is how many bytes you want to read.
You may want a simpler solution, such as reading the entire file and splitting it into words. This would look something like:
file_contents = animals.read() animalList = file_contents.split()
If split is new to you, then (look up)[https://docs.python.org/2/library/string.html] that method as well.
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:
animalList = [animal1, animal2, animal3, animal4, animal5]