To save Int to a list

Asked 2 years ago, Updated 2 years ago, 20 views

def times(num):
    i = 1
    ll = []
    while i < 10:
        result = num * i
        ll = [result.append()] #problem
        i = i + 1
    return ll

Hi, everyone. If you enter a number of num in the function times(num), it would be nice if the number multiplied by 1 to 9 was included in the list.

Run the code above
It says 'int' object has no attribute 'append'. In this case, how can I put the int in the list ll? Thank you.

For your information, Jump to Python provided the following code.

def gugu(n):
    result = []
    i = 1
    while i < 10:
        result.append(n*i)
        i = i + 1
    return result

python

2022-09-22 15:19

1 Answers

You must invoke the append() method in the list ll.

append() adds the call factor to the end of the list.

def times(num):
    i = 1
    ll = []
    while i < 10:
        result = num * i
        ll.append(result) #problem
        i = i + 1
    return ll


2022-09-22 15:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.