Reputation: 11070
Can inputting and checking be done in the same line in python?
Eg) in C we have
if (scanf("%d",&a))
The above statement if block works if an integer input is given. But similarly,
if a=input():
Doesn't work in python. Is there a way to do it?
Upvotes: 1
Views: 725
Reputation: 304137
You can't do it. This was a deliberate design choice for Python because this construct is good for causing hard to find bugs.
see @Jonathan's comment on the question for an example
Upvotes: 6
Reputation:
No, Python can't do assignment as part of the condition of an if
statement. The only way to do it is on two lines:
a=input()
if a:
// Your code here
pass
This is by design, as it means that assignment is maintained as an atomic action, independent of comparison. This can help with readability of the code, which in turn limits the potential introduction of bugs.
Upvotes: 9