0

Whenever my algorithm inputs a two digit number into my list such as 10, it inputs it as a = [1, 0]and not a = [10] How can I stop this?

My program is meant to take the user's number and generate x amount of generations of the Fibonacci sequence. (1, 1, 2, 3, 5, 8...)

Heres my code:

def get_integar(help_text):
    global num
    num = int(input(help_text))
a = []
get_integar("Enter number of generations: ")
j = 0
x = 0
sum_jx = 0
for i in range(num):
    if j == 0 and x == 0:
        j = 1
        a.extend(str(j))
        x = 1
        a.extend(str(x))
    else:
        sum_jx = j + x
        a.extend(str(sum_jx))
        x = j
        j = sum_jx
    i = i + 1
print(a)

for example here is one outcome with 7 generations:

Enter number of generations: 7
['1', '1', '2', '3', '5', '8', '1', '3', '2', '1']

it should be

['1', '1', '2', '3', '5', '8', '13', '21']

  • First of all, if you entered `10`, `num` will be `10` not `[1,0]` as you said. Second of all, your algorithm of Fibonacci is wrong... check [this](https://www.programiz.com/python-programming/examples/fibonacci-sequence) out. – Anwarvic Apr 28 '20 at 10:00
  • Did not get what you are actually asking for – Nitheesh MN Apr 28 '20 at 10:01
  • Ive tested the algoritim it works, the only problem is when the algorithim places a two-digit number (for example 10) in my list it puts it in like ' a = [1, 0 ]' and it should be 'a = [10]' – justwhateverbro Apr 28 '20 at 10:03

2 Answers2

1

Try using append instead. Extend will see your string as a iterable. A string as a iterable is just the list of its characters. Also, the first two numbers should be 0 and 1, not 1 and 1 and there is a typo in the name of the function.

def get_integar(help_text):
    global num
    num = int(input(help_text))
a = []
get_integar("Enter number of generations: ")
j = 0
x = 0
sum_jx = 0
for i in range(num):
    if j == 0 and x == 0:
        j = 1
        a.append(str(j))
        x = 1
        a.append(str(x))
    else:
        sum_jx = j + x
        a.append(str(sum_jx))
        x = j
        j = sum_jx
    i = i + 1
print(a)

Documentation for extend and append from https://docs.python.org/3/tutorial/datastructures.html:

list.append(x) - Add an item to the end of the list. Equivalent to a[len(a):] = [x]. list.extend(iterable) - Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.

0

You can use .extend() but in another way, simply replace a.extend(str(x)) to a.extend([str(x)]) (extends works like arr1+arr2). See examples here: What is the difference between Python's list methods append and extend?.

def get_integar(help_text):
    global num
    num = int(input(help_text))
a = []
get_integar("Enter number of generations: ")
j = 0
x = 0
sum_jx = 0
for i in range(num):
    if j == 0 and x == 0:
        j = 1
        a.extend([str(j)])
        x = 1
        a.extend([str(x)])
    else:
        sum_jx = j + x
        a.extend([str(sum_jx)])
        x = j
        j = sum_jx
    i = i + 1
print(a)

EDIT: more nature way is to use .append(elem) and then map(str, arr1).

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24