5

I am trying to call a function in an if, save the value in a variable and check if the value is equal to something. Have tried:

def funk():
    return ("Some text")

msg=""
if (msg=funk()) == "Some resut":
  print ("Result 1")

I am getting a syntax error. Can this be done?

I'm coming from C and I know that this can work:

#include <stdio.h>

char funk()
{
    return 'a';
}
int main()
{
    char a;
    if( a=funk() == 'a' )
        printf("Hello, World!\n");
    return 0;
}

Is this possible in python?

P.S. The functions are simple so it is easier to understand what I want. I will not use the some functions in my program. If you give me a MINUS please explain in comment so I can make better questions in the future

ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
kemis
  • 3,516
  • 5
  • 23
  • 38
  • 1
    Possibly better duplicate that's specific to `if` (but the same rules apply to all conditionals in Python, `if`, `elif`, `while`, so the one I linked as duplicate which has more attention works too): [Assign within if statement Python](http://stackoverflow.com/q/30066516/364696) – ShadowRanger Nov 29 '16 at 02:22
  • Also [Can I assign the value of the left hand side of a Boolean expression (e.g in a while loop) to something?](http://stackoverflow.com/q/39855895/364696). This has been covered a _lot_. – ShadowRanger Nov 29 '16 at 02:24

1 Answers1

10

Python purposely doesn't support it because it hurts readability. It needs to be two lines.

msg = funk()
if msg == "Some result":
    print("Result 1")
John Kugelman
  • 307,513
  • 65
  • 473
  • 519