how to convert an int from user input into a list?

I am a newbie in Python. I want to know how to convert input from user into a list. I tried this code but the result is an error...

a = int(input('Masukkan angka pertama : '))
b = int(input('Masukkan angka kedua : '))
c = int(input('Masukkan angka ketiga : '))

if a < b < c :
    print (list(a, b, c))

4 answers

  • answered 2021-05-05 05:56 Abhyuday Vaish

    You can use :

    list1 = []#Creating empty list
    a = int(input('Masukkan angka pertama : '))
    b = int(input('Masukkan angka kedua : '))
    c = int(input('Masukkan angka ketiga : '))
    list1.append(a)#Adding user input to list
    list1.append(b)
    list1.append(c)
    print(list1)#Printing list
    

    You can learn more about lists.

    https://www.geeksforgeeks.org/python-list/

  • answered 2021-05-05 05:58 the__hat_guy

    list() accepts an iterable and while you are passing integers. So the last line should be

    print(list[a, b, c])
    

  • answered 2021-05-05 06:00 Mayank Kashyap

    Try :

    print([a,b,c])
    

    The list() function works when you wish to convert another sequence(eg string, tuple etc.) into a list.

  • answered 2021-05-05 06:27 DonKnacki

    Your syntax is nearly good. Problem is that list() expects only one parameter which must be an iterable. So you can use a tuple or a list(strange but why not)

    print(list((a, b, c))) 
    print(list([a, b, c])) 
    

    You can also avoid to use list() function

    print([a, b, c]) 
    

    And you can use append as suggested by @Abhyuday Vaish