Input problem

why do I need to take input like this:
num1,num2 = map(int,input().split())
when i can just take input like that:
num1=int(input())
num2=int(input())
but this value error

The input is given in a single line spaced and hence, you gotta use map.

we use map to get input in single line and spaced rather than in different line

1 Like

Because the input provided is a single line spaced.

The map take values entered after space, in a single line. Whereas assigning it to other line, result in taking input in two lines.

map() takes two arguments. The first one is the method to apply, the second one is the data to apply it to. By this understanding, we can see this is doing nothing but typecasting every element of the list to an integer value.

difference will be there ,as in case 1 map takes two arg. as in your case it takes the input for two space separated variables and they have to be integer.
whereas, the second takes output in two different lines

the input is given in a single line whereas your program takes input in two lines. we use the map function to unpack the values and assign it to x and y.

We can use this method to unpack multiple values. learn about map first then try to solve the questions.

Because bro map is used to give output in single line with one space, and when you will use it differenty it not give output in one line and also you have to use because in testcase they had said to give output in one line

In order to get input in single line map is used rather than seperate lines

in order to decrease the lines of code we will use the map function.
for example here you just took only 2 inputs that’s why it just take two lines ,but for many inputs also if we use map function it takes only two lines to write the code.

  • Use map(int, input().split()) for space-separated inputs in one line.
  • Use separate int(input()) calls for individual inputs on separate lines.

we use map() to take input of variables in a single line with space
we also use map function to convert the input in list

Using num1, num2 = map(int, input().split()) lets you enter both numbers in one line, which is convenient. If you try to use num1 = int(input()) and num2 = int(input()) , you have to enter each number separately on different lines. If you put both numbers on the same line, you’ll get a ValueError because it tries to convert the whole line into a single integer. The first method just makes things easier and faster! hope it helps… :smiling_face:

Because we have to split the given input in between space

That takes the spaced integer input from the user in the single line whereas the second one takes two different integer inputs in different lines .