0

I wanted to ask if we can add if statements inside a variable in python

I was building a program in python this was it's code-

a2 = 2
a3 = 3
def myfunc(a):
    a = 0
    b = int(input("your number?"))
    c = if a2 % b == 0 or a3 % b == 0
           return a
    print(a)
myfunc()

for some reason it was declaring syntax error in 6th line of my code. So what should I do to make my code work fine and whether there are some more errors in it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    There is no such thing as an 'if statement inside a variable'. Not even in this code, whatever it's meant to do, which only you can tell us. It doesn't make any sense to me. – user207421 Aug 08 '23 at 05:00
  • 1
    The posted code doesn't make sense. Python does have a conditional expression, `a if c else b`, which evaluates to `a` if `c` is true, otherwise it evaluates to `b`. But I can't see any rational interpretation of the posted code. – Tom Karzes Aug 08 '23 at 05:09
  • 1
    Also, inventing your own syntax, trying it, then asking why it doesn't work is a poor way to learn a programming language. It doesn't work because it's not part of the language that you haven't learned. – Tom Karzes Aug 08 '23 at 05:10
  • 1
    @TomKarzes *at the level of background knowledge* that someone *who needs the question answered* is likely to have, it's a reasonable way *to communicate the intent of the actual feature*. It's just... if it exists, it's surely a duplicate (as I've marked it), and otherwise it at least needs to be edited by someone more competent, so as to properly express what was communicated. (Ideally, still in a way comprehensible to beginners.) – Karl Knechtel Aug 08 '23 at 05:35
  • @KarlKnechtel Not if nobody understands it. I've been programming in dozens of languages for 52 years and I have no idea what he is trying to do. Nobody else above understood it either. Do you? – user207421 Aug 08 '23 at 05:40
  • 1
    @KarlKnechtel That would make sense if the code had some meaningful interpretation, which in this case it doesn't. It's a statement that contains a `return`, whose non-existent result value is assigned to a variable. If the condition is true, should it assign `a` to `c`? What if it's false, what should it do then? – Tom Karzes Aug 08 '23 at 05:43
  • 1
    "I've been programming in dozens of languages for 52 years and I have no idea what he is trying to do. Nobody else above understood it either. Do you?" - I mean, do you think I picked the wrong target for duplicate closure? It was the first thing that came to mind, and it just happens to agree with the interpretation from the one answer that was offered. – Karl Knechtel Aug 08 '23 at 06:14
  • @KarlKnechtel That's a question in answer to my question. You could always just answer the question I asked, instead of all this futzing around. The possibility of a ternary expression had already been raised but questions about that interpretation remain unanswered. – user207421 Aug 08 '23 at 06:24

1 Answers1

0

You can try this:

a2 = 2
a3 = 3

def myfunc():
    a = 0
    b = int(input("Your number?"))
    c = a if a2 % b == 0 or a3 % b == 0 else None
    print(c)

myfunc()
pr_kr1993
  • 185
  • 6