-1

I would like to take the input of the list all in the same line. However i get the following error

> val = [int(input().split())]
> TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

here is my code

n=int(input("Enter an input: "))
val=[]
val = val[:n]  
val = [int(input().split())]
print (val)
DYZ
  • 55,249
  • 10
  • 64
  • 93
Srinabh
  • 400
  • 5
  • 13

1 Answers1

0

If I understand your question correctly, you want to your list using successive inputs. Here is something that may be what you are looking for:

ans = None
values = []
while ans != '':
    ans = input('enter value: ')
    if ans.isdigit():
        and = int(ans)
    if ans.strip() == '':
        break
    values.append(and)
print('\n'.join(str(x) for x in values)

This will take inputs until the input is blank, converting any input that is a number to an int since input return values are always strings

Alternatively you can directly get a list of space separated numbers. This approach will fail if a non-digit value is entered:

print('enter space-separated numbers')
values = [int(val) for val in input().split()]
N Chauhan
  • 3,407
  • 2
  • 7
  • 21