jeudi 14 mars 2019

End 'If Statement' with Function

I am trying to basically pass over the rest of an if statement once a function is run to determine a certain value to be 0 or 1. If the value is 0, It passes the rest of the if statement, if it's 1, it continues with the if statement it's in and run accordingly. Here's the code:

test2.py

import os, sys
from functiontest import function

message = 'foo'
check = 0

if 'foo' in message:
    check = function(message,check)
    print(check)
    print('bar')

if check == 0:
    print('foo not recognized')

with the function file being

functiontest.py

import os, sys

def function(a,b):
    print('checking message')
    a = a.split()
    print(a)
    if a[0] == 'foo':
        b = 1
        print(b)
        return b
    else:
        b = 0
        return b

When a word other than "foo" exactly as written is detected, it should only output "foo not recognized". With a word like "foot", it still runs the code within the if statement because it contains foo, which I don't want it to. Is there a way to pass over the rest of the if statement from the function side? I know how to do it with an if else statement in the main test2.py code because I pass back the check argument, but I'm looking to make the main code as clean as possible, so is there a way to accomplish that within functiontest.py? Thanks.

Aucun commentaire:

Enregistrer un commentaire