'for loop error '>=' not supported between instances of 'str' and 'int'

I want to make a for loop to do multiplications, and add + and - signs to each number.

n = int(input("n : "))
list_num = [1]
a = 1

for i in range(n-1):
    a = a*-5
    if(a >= 0):
        a = '{0:+}'.format(a)
    list_num.append(a)

for x in list_num:
    print(x, end=' ')

I want to get a result like:

+1 -5 +25 -125 +625 -3125

but my Python code error like this

if(a >= 0):
TypeError: '>=' not supported between instances of 'str' and 'int'

Please help to be able to resolve the error



Solution 1:[1]

You are assigning a str to a:

if(a >= 0):
        a = '{0:+}'.format(a)
    list_num.append(a)

replace by

list_num.append('{0:+}'.format(a))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1