Berke Balcı
Berke Balcı

Reputation: 93

How can i use input().split() effectively to take at least 2 inputs from user in one line?

I was reading an article about python and i saw input().split().I think,I can take multiple inputs from users with thatAnd I wrote this code.

a,b = input("Enter your number").split()
print(a,b)

But I got confused because whenever I try to run this code I got this error:

not enough values to unpack (expected 2, got 1)

So,How can i take multiple inputs with that or can i?

Upvotes: 2

Views: 3019

Answers (1)

Malo
Malo

Reputation: 1313

In your example you probably typed just one number on the line, and the error was raised because you expect 2 (a and b)

You should type both numbers on the same line, then enter.

You can change your code to accept any number of numbers, and store them in a list:

res = input("Enter your numbers:").split()
print(*res)   ## prints the list content
for r in res: ## prints number one by one and one per line
    print(r)

result:

Enter your numbers: 12 13 14
12 13 14
12
13
14

Upvotes: 4

Related Questions