difflib, type error int object is not iterable

Viewed 261

Link to the code

import random
import difflib
number = int(input("How many words do you want to practise?"))
words = [*3000 word array*]

for x in range(0, number):
    text_1 = random.randint(0, 3000)
    z = words[text_1]
    print(z)
    text_2 = str(input("Type:"))
    seq = difflib.SequenceMatcher(isjunk=None, a=text_1, b=text_2)
    difference = seq.quick_ratio()
    difference = round(difference, 1)
    print(str(difference) + "% Match")

 print("Thank you!")

The Error message I kept getting: (Line 12)

for elt in self.a: TypeError: 'int' object is not iterable

I have been on a good flow with this program but reached this wall and have tried so many different ways of figuring it out but really couldn't. The program is meant to be a typing test/practice thing. I hope anyone here can help me figure out a solution for my error message, or any suggestions, in general, would be great.

Note: I am very new to this website, so I apologize if any 'format' I use is wrong.

1 Answers

I looked at the difflib.SequenceMatcher documentation, and it seems like a and b need to be sequences. You are giving it an int (text_1). If think you meant

seq = difflib.SequenceMatcher(isjunk=None, a=z, b=text_2)

Note: you should use random.choice to choose a random item from a list, like this z = random.choice(words) that way you dont need text_1

Related