v7676
v7676

Reputation: 33

Cannot understand these errors and outputs in python code

print("Enter number of test cases and length of each string")
t,s=map(input,range(2))
print(t,s)

It prompts as Enter number of test cases and length of each string 03 13. Why is there 0 and 1 before 3's?

The below line shows ouput

t,s=map(lambda x :input ,range(2))
 OUTPUT-<built-in function input> <built-in function input>
WHY IS THAT?

Whereas this line :

t,s=map(int(input()),range(2))
OUTPUT: Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(int(input()),range(2))
TypeError: 'int' object is not callable.

WHY IS IT NOT CALLABLE?

t,s=map(input(),range(2))

THE ABOVE LINE HOWEVER CAUSES THIS ERROR:
Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(input(),range(2))
TypeError: 'str' object is not callable.

WHY IN THE FIRST CASE NO ERROR AND IN LAST ONE THERE IS.
python
t,s=map(lambda x: int(input()),range(2))


ALSO THIS ONE WORKS JUST FINE.
WHY USING LAMBDA MAKES IT WORK FINE BUT WITHOUT LAMBDA AS IN THE 3rd ONE IT THROWS ERROR.

Upvotes: 0

Views: 68

Answers (1)

Weeble
Weeble

Reputation: 17910

print("Enter number of test cases and length of each string")
t,s=map(input,range(2))
print(t,s)

You can understand this by breaking it down into pieces.

range(2) returns a sequence of two numbers, starting from 0. So the first number in the sequence will be 0 and the second will be 1.

map(f, sequence) will call function f once for each item in sequence, and collect the results into a new sequence. Since we have map(input, range(2)) it will call the function input twice, once with 0 as input and once with 1 as input, i.e. input(0) then input(1).

Each time input(prompt) is called, it displays the prompt and waits you to type an input string. So the first time the prompt is 0 and the second time the prompt is 1.


t,s=map(lambda x :input ,range(2))
 OUTPUT-<built-in function input> <built-in function input>

WHY IS THAT?

lambda x: input defines a lambda-function that takes one argument (x) and returns the function input as a value. Functions can be passed around as values, indeed we want to give map a function as its first argument. But in this case we probably don't want to return the function input as a value when our lambda-function is called. Instead we want to call it. To call a function with no arguments, you write a pair of parentheses with nothing between them. E.g. input(). So here's what you could write instead:

t,s=map(lambda x :input() ,range(2))

t,s=map(int(input()),range(2))
OUTPUT: Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(int(input()),range(2))
TypeError: 'int' object is not callable.

The first argument to map should be a function, because map itself will call it (repeatedly). But in this case, rather than passing it a function we are passing it a number. You can try instead:

t,s=map(lambda x :int(input()) ,range(2))

In this case the lambda function tells Python that we don't want to evaluate int(input()) immediately, but instead to create a function that will do that when it is called.


t,s=map(input(),range(2))

THE ABOVE LINE HOWEVER CAUSES THIS ERROR:

Traceback (most recent call last):
  File ".\gridchallenge.py", line 2, in <module>
    t,s=map(input(),range(2))
TypeError: 'str' object is not callable.

WHY IN THE FIRST CASE NO ERROR AND IN LAST ONE THERE IS.

This is again the same issue. map(input(), range(2)) will call the input function straight away and pass its return value (a string) to map as an argument, whereas map(input, range(2)) passes the function input itself as an argument.

Upvotes: 1

Related Questions