简体   繁体   中英

Python checking several if-conditions (find & replace word with number)

I want to find a string (blue or red) and turn it into a number (1 or 2). I have in my Code 2 if-Conditions. Both of them are true. So I should get as an output the number 1 and 2.
But depending on that where I put return, I always get either 1 or 2, but never 1 and 2 at the same time. What am I missing? Is it not possible to have 2 if-conditions at the same time?
My Input-Text looks for example like this:

myInput = "I like the colors blue and red."

def check_color(document):

    for eachColor in document:

        if ("blue" in myInput):
            color_status = "1"
            #return color_status # only 1 as result

        if ("red" in myInput):
            color_status = "2"
            #return color_status # only 1 as result
        else:
            color_status = "0"
            #return color_status # only 1 as result
    #return color_status # only 2 as result

Without any return --> Output: None

function call

color_output = check_color(myInput)
print(color_output)

Of course you need a return statement to get any result at all. The problem is in the concept: If you want to return zero or more values, a list would be the easiest solution. (Your code simply overwrites the 1 by the 2).

   color_status = []
   if "blue" in myInput:
        color_status.append("1")

   if "red" in myInput:
        color_status.append("2")

   return color_status

This is one approach.

Demo:

myInput = "I like the colors blue and red."

def check_color(document):
    clr = ["blue", "red"]
    color_status = 0
    if all(i in document for i in clr):
        color_status = [1, 2]

    elif ("red" in myInput):
        color_status = 2
    elif ("blue" in myInput):
        color_status = 1

    return color_status

color_output = check_color(myInput)
print(color_output)
  • Using all to check if all colors in text
  • elif to check the other conditions.

So I should get as an output the number 1 and 2.

No. Once your function reaches a return statement, a value is returned and the function goes no further.

Is it not possible to have 2 if-conditions at the same time?

Yes. But if you put a return statement in an if clause which evaluates to True , all subsequent if clauses will be ignored.

Without any return --> Output: None

Yes. If your function does not return or yield anything, it will return None .


You need to carefully define what you want as your output in each situation. For example, if you want a list of values as output, initialize a list and append to it. A dictionary will make your solution easiest to implement and extend.

Here's a demo:

myInput = "I like the colors blue and red."

def check_color(var):

    c_map = {'blue': 1, 'red': 2}

    L = []
    for colour, num in c_map.items():
        if colour in var:
            L.append(num)

    return L

print(check_color(myInput))

[1, 2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM