reshmi g
reshmi g

Reputation: 151

python function to get the number of values read using input() function

In C language, the scanf() function will return the number of data read into the variables. Is it possible to get the number of variables read using input() function in python? for eg.

v = scanf("%d%d%d", &a, &b, &c);

returns the number of integer variable read using scanf() function. If the three variable values are not integers it will return the value less than three.

Upvotes: 1

Views: 190

Answers (2)

RiaD
RiaD

Reputation: 47620

You may need

len(raw_input().split())

It doesn't check arguments for being integers through. You may use

numbers = map(int, raw_input().split())

to cast input to integers. You will get an exception (ValueError) in case of wrong format

Upvotes: 2

user2357112
user2357112

Reputation: 280733

raw_input() reads exactly 1 line. If you want to know what was in the line, you can do the usual things: call len, split it, try to convert it into an int, etc.

input() should not be used; it's equivalent to eval(raw_input()), and malformed or malicious input can do nasty things to your program if you use it. If you really want to use input(), you can inspect the value it returns the usual ways.

Upvotes: 0

Related Questions