0

I have two functions with one containing a return statement. My agenda is when the first function executes in the if condition, compiler is throwing a syntax error.

Here's my script.

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

if a,b=hi() : hello(a)

The error is thrown at "=" in the if condition. How to solve this. Or is there any alternative to perform this in one liner?

2 Answers2

2

You need to create a tuple of a and b then you can compare using ==

Use this,

if (a,b)==hi() : hello(a)

Here is the full code which prints asd

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

a,b = 10,20

if (a,b)==hi() : hello(a)
Saeed
  • 142
  • 9
  • But what if the return values inside the function ```hi()``` is dynamic?. –  Jul 01 '19 at 11:31
  • so should I use a global tuple to have a dynamic value? –  Jul 01 '19 at 11:35
  • I don't think you need global tuple. If the return value inside `hi()` function is, let's say `return x,y` where x and y are variables which you can set before, your if condition will simply check the actual value tuples, if they are equal or not. For example, if `a,b=10,20` and `x,y=5,10` then your if will evaluate to look like `if (10,20)==(5,10)` which will be false and `hello(a)` won't be executed. – Saeed Jul 02 '19 at 02:42
0

You can't have arbitrary expressions in an if statement, you can only use a boolean like value (something that can be interpreted as true/false).

So you need to do something like this

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

a, b = hi() 
if a:
    hello(a)
abdusco
  • 9,700
  • 2
  • 27
  • 44