MeowWOW
MeowWOW

Reputation: 41

Python: TypeError: 'int' object is not iterable - in For Loop

OK, I have looked at many questions to do with this topic yet I couldn't find a specific answer to my queries. I am having and TypeError which has been bothering me. In my code I've been trying to hash a string very naively but my challenge is to do it without any hash libraries and with basic libraries such as 'random' or 'time' (don't know why that is useful). Here is my code so far:

import random
char_array = "Hello World!"
hash_lvl = random.getrandbits(15)

def hash (lvl, string, len_string):
    a = 9
    b = 2
    new_array = []

    for d in range(0, len_string):
        new_array.extend(d)
    for c in range(0, len_string):
        globals()['string%s' % c] = (lvl/a)+(lvl*b)
        a=a-1
        b=b+1

print(char_array[0:])

if len(char_array) > 20:
    print("You may not hash after 20 digits.")
elif len(char_array) < 21:
    print("Hashing:")
    hash(hash_lvl, char_array, len(char_array))

The for loops inside the functions have caused this so if you could get back to me I would be grateful.

Upvotes: 0

Views: 2383

Answers (2)

ballade4op52
ballade4op52

Reputation: 2267

The extend method is expecting an iterable (e.g. a list, tuple, or string), which would add each of the items in the iterable to the end of the list. The append method adds a single item to the end of the list (an iterable or non-iterable).

Upvotes: 1

Ilja Everil&#228;
Ilja Everil&#228;

Reputation: 52929

Replace

for d in range(0, len_string):
    new_array.extend(d)

with

for d in range(0, len_string):
    new_array.append(d)

list.extend extends a list in place, list.append adds an item at the end.

Upvotes: 2

Related Questions