-1

I am trying to break out of an if statement within a function, but it is not behaving as I expect. I am following answers on this previous SO question, How to exit an if clause, specifically the top rated answer.

Here is the code:

import random

def logic_function():
    if 1 == 0:
        random_number = random.randint(10, 20)
        return random_number
    return

def number_function(foo):
    if foo == 1:
        number = logic_function()
        print(number)
    elif foo == 2:
        number = 2
        print(number)
    else:
        print('Nothing will happen')

print(number_function(1))

What I expect:

I have a function, number_function, where a number is inputted as a parameter. If the parameter is 1, then the generated number uses a function called logic_function to generate a random number between 10 and 20, if 1 == 0. 1 will not be equal to 0, so I expect the the return at the end of the logic_function to be called and I go into the outer if else statements within number_function, specifically the else statement at the end where Nothing Will Happen is printed.

Current output for print(number_function(1)) is:

None
None

I expect it to be:

Nothing Will Happen

I am also curious why that second None is being printed. Let me know if more clarification is needed.

W. Churchill
  • 346
  • 1
  • 7
  • 28
  • 1
    In your example, the program behaves exactly as it should: You set `foo==1` in your function call. So, the first condition of your if-block is true and the `logic_function` is called. This function can only return `None` as 1 never is equal to 0. This `None` is printed. Then, you print the result of the `number_function()`, which is `None` because this function does not return anything. That's where the two `None` come from. – Martin Wettstein Aug 14 '20 at 08:27
  • 1
    This is a very strange question. When `foo == 1`, then whatever is indented under `if foo == 1:` is executed. What does `logic_function` have to do with any of this? – timgeb Aug 14 '20 at 08:28
  • Your code has some inaccuracy . if 1 == 0: This will always be False so none will come everytime . Try to wrestle with problems more. – devansh Aug 14 '20 at 08:28
  • 1
    "so I expect the the return at the end of the logic_function to be called **and I go into the outer if else statements within number_function**" <- why do you expect the bolded part to happen? – timgeb Aug 14 '20 at 08:31
  • @timgeb This is where I had issues, I do not know if I have to explicitly tell the statement to break and go to the outer `else`. This can be done in a `while` with `break`, but this is not a `while` loop, so I am uncertain how to approach this. – W. Churchill Aug 14 '20 at 08:35
  • 1
    If makes zero sense to go into the `else` block of an `if foo == 1: ... / else: ...` statement when `foo` is equal to one. – timgeb Aug 14 '20 at 08:37
  • @timgeb Thank you for the feedback, I am trying to learn why this would make "zero sense". I assumed I would go to the outer `else` because conditions within the `logic_function` are not being met, I am now realizing this is not the case. – W. Churchill Aug 14 '20 at 08:41
  • 1
    The test you are performing in order to decide whether to go into the `if` or `else` block is `foo == 1`. There is no other test. `logic_function` is completely irrelevant for testing which block to enter. – timgeb Aug 14 '20 at 08:43

2 Answers2

0

You're assumptions are wrong. First of all the function logic_function will always return None. As 1 == 0 is always False, you could just simplify it to:

def logic_function():
    return

Second, if you have an if-elif-else construct, you will always run into one single branch, i.e. you won't execute the statements in the if part and the statements in the else parts. That's not possible.

You're program prints None twice, because you have one print inside the if branch and one after the whole if-elif-else construct.

As you seem to not understand basic python concepts, I recommend you to make some tutorials about it, e.g. this one

AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33
0
def logic_function():
    return

def number_function(foo):
    if foo == 1:
        number = logic_function()
        print(number)
    else:
        print('Nothing will happen')

print(number_function(1))

I removed some unnecesary code.

your number_function doesn't return anything. So if you print that it will be None.
You supply the function with a 1. So your logic_function will be executed. That function returns None. Which you save in the variable number which is then printed.

So it's logical for your code to print None twice. It will not print Nothing Will Happen because the condition foo == 1 evaluates to True.

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32