jee
jee

Reputation: 11

Python taking input as list of integers

I am trying to take input as list of integers. Here is my attempted code

input_binary = int(list(input("enter a binary number: "))) # taking a user input as integers

Here is the error it is throwing

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Have any idea?

Upvotes: 0

Views: 62

Answers (2)

Ray
Ray

Reputation: 1676

If you want to have input "123456" and output [1,2,3,4,5,6]

input_string = [int(num) for num in input("enter a binary number: ")]
print(input_string)

Result:

enter a binary number: 123456
[1, 2, 3, 4, 5, 6]

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24711

You can't convert a list into an integer (at least, not with int()), which is what you're trying to do. Instead, try doing things in the other order.

Say you want a list of 5 integers:

binary = []
for _ in range(5):  # do the following 5 times
    inp = int(input("enter a binary number: "))  # take user input as string, convert to int
    binary.append(inp)  # put that int into our list

Upvotes: 1

Related Questions