2

Hi could someone help with this code,i am getting the error: " 'int' object is not iterable " at line 28(A.extend(n)) since i am new to python i am unable to figure out a solution any help is appreciated

Edit:I tried append earlier and got a memory error earlier and was wondering if extend() was the correct way to add elements but it seems like I made a mistake and the ended up with a infinite loop Thanks for the advice it really helped me

print("Ax^2+Bx+C")
a = int(input("a"))
b = int(input("b"))
c = int(input("c"))
i, j, k, l = 0, 0, 0, 0
A = []
C = []
B = []
ano = []  
bno = []  
no = 0
noc = 0  
n = 2
a2 = a
c2 = c

if (a != 1) or (b != 1):
while i != 1:
    while a2 % n == 0 and c2 % n == 0:
        if a2 % n == 0:
            a2 /= n
            # A.extend(n)
            no += 1
        if c2 % n == 0:
            c2 /= n
            # A.extend(n)
            no += 1
    A.extend(n)
    ano.extend(no)
    no = 0
    n += 1
    if a2 == 1:
        A.extend(1)
        A.extend(1)  
        i = 1
yade123
  • 71
  • 1
  • 1
  • 4
  • 2
    `extend` requires a list as the argument. You are passing in a single value. You probably want to use `append()` instead. – pault May 11 '18 at 16:49
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. – Prune May 11 '18 at 16:53
  • "extend" Extends list by appending elements from the iterable, but you are giving an integer to extend. Instead use append() function to add a single value to a list – Naren Babu R Oct 10 '19 at 06:33

1 Answers1

7

You are looking for append not extend

>>> a = []

list.extend does not work with a single item

>>> a.extend(1)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    a.extend(1)
TypeError: 'int' object is not iterable

list.append adds the item to the end of the list

>>> a.append(1)
>>> a
[1]

The purpose of list.extend is to add another list to the end of the current one, for example

>>> a.extend([2,3,4])
>>> a
[1, 2, 3, 4]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218