TypeError: an integer is required I cant figure it out

Question

I have been working on this code for a while, and this error poped up. And I dont know alot about it...

minimun = raw_input("Minimum length of any give word to be generated: ")
maximun = raw_input("Maximum length of any give word to be generated: ")
maximunWords = raw_input("Maximun number of words to be generated in the diccionary: ")
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYX0123456789'
string = ""


create = open('wordlist.txt', 'w')
print "Creating..."
time.sleep(2)
print "Start Time: ", time.strftime('%H:%M:%S')

for i in xrange(0,maximunWords):
    for x in random.sample(alphabet,random.randint(minimun,maximun)):
        string+=x

create.write(string+'\n')
string = ""
create.close()
print "End Time: ", time.strftime('%H:%M:%S')

This especific block is givin me this error

for i in xrange(0,maximunWords):
    for x in random.sample(alphabet,random.randint(minimun,maximun)):
        string+=x

The error says this:

File "ACU-Tool.py", line 62, in <module>
    for i in xrange(0,maximunWords):
TypeError: an integer is required
enter code here

Show source
| python   | integer   | for-loop   | typeerror   2017-01-03 04:01 2 Answers

Answers to TypeError: an integer is required I cant figure it out ( 2 )

  1. 2017-01-03 04:01

    The reason you are receiving a TypeError is because maximunWords is not of type integer, but rather of type string. You need some integer for the second parameter in xrange since you need some integer to determine when to "stop" the range.

  2. 2017-01-03 04:01

    The problem is that maximunWords isn't an integer; it is a string returned by raw_input(). Obviously, the xrange function needs integer arguments, not strings.

    Change the code to convert the user input to integers and the code will work better:

    minimun = int(raw_input("Minimum length of any give word to be generated: "))
    maximun = int(raw_input("Maximum length of any give word to be generated: "))
    maximunWords = int(raw_input("Maximun number of words to be generated in the diccionary: "))
    

Leave a reply to - TypeError: an integer is required I cant figure it out

◀ Go back