Jump to content

Py3: Detect string in input and act on it

LewisSpring
Go to solution Solved by WereCatf,
Just now, TheLewisS1 said:

3.7.3. Just found it's a 3.8 feature. I'll see if i can update.

Sorry, I forgot that the walrus-operator was only introduced in 3.8. For earlier Python-versions, it'd be:

eventsList = ["FIRE", "TAMP", "INTRUDER", "FAULT"]
event = input()
if(event in eventsList):
	decipher_act(event, 1)

Not a big difference, but I like using the walrus-operator because it makes the code ever slightly more readable.

 

Hello.

 

I need to trigger an event if an function's input string is equal to a variable.

So far I have

    event_fire = "FIRE"
    event_tamper = "TAMP"
    event_intruder = "INTRUDER"
    event_fault = "FAULT"
    ...
    
    elif input.find(event_fault) != -1: decipher_act(event_fault,1)
    elif input.find(event_intruder) != -1: decipher_act(event_intruder,1)
    
    elif input.find(event_tamper) != -1: 
    	decipher_act(event_tamper,1)
    ...

Which works: But I feel there has to be a better way than if statements, as there is 15+ of these in my code.

 

Any ideas?

 

Thanks!

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, TheLewisS1 said:

Any ideas?

eventsList = ["FIRE", "TAMP", "INTRUDER", "FAULT"]
if((event := input()) in eventsList):
	decipher_act(event, 1)

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, WereCatf said:

eventsList = ["FIRE", "TAMP", "INTRUDER", "FAULT"]
if((event := input()) in eventsList):
	decipher_act(event, 1)

 

Hi there.

:= is a syntax error.

Thanks! 

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheLewisS1 said:

Hi there.

:= is a syntax error.

Thanks! 

What Python-version are you using?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, WereCatf said:

What Python-version are you using?

3.7.3. Just found it's a 3.8 feature. I'll see if i can update.

Thanks!

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, TheLewisS1 said:

3.7.3. Just found it's a 3.8 feature. I'll see if i can update.

Sorry, I forgot that the walrus-operator was only introduced in 3.8. For earlier Python-versions, it'd be:

eventsList = ["FIRE", "TAMP", "INTRUDER", "FAULT"]
event = input()
if(event in eventsList):
	decipher_act(event, 1)

Not a big difference, but I like using the walrus-operator because it makes the code ever slightly more readable.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, WereCatf said:

Sorry, I forgot that the walrus-operator was only introduced in 3.8. For earlier Python-versions, it'd be:



eventsList = ["FIRE", "TAMP", "INTRUDER", "FAULT"]
event = input()
if(event in eventsList):
	decipher_act(event, 1)

Not a big difference, but I like using the walrus-operator because it makes the code ever slightly more readable.

Brilliant! I didn't really want to risk borking my install. You're a lifesaver. Thanks!!

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, TheLewisS1 said:

Brilliant! I didn't really want to risk borking my install. You're a lifesaver. Thanks!!

@WereCatfJust been playing around - this might not work, since the input contains other characters other than the string i need to look for. EG

00:00 UNSET       USR000 NAME    M22 Lx.x

Could I be a pain and ask for any ideas you may have? Closest I have found is 

print([ele for ele in eventsList if(ele in input)] )

but that outputs

['UNSET', 'KSW UNSET']

if there are two matches, and that's not too ideal since they are different events.

 

Sorry and Thanks!

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

58 minutes ago, TheLewisS1 said:

@WereCatfJust been playing around - this might not work, since the input contains other characters other than the string i need to look for. EG




00:00 UNSET       USR000 NAME    M22 Lx.x

Could I be a pain and ask for any ideas you may have? Closest I have found is 




print([ele for ele in eventsList if(ele in input)] )

but that outputs




['UNSET', 'KSW UNSET']

if there are two matches, and that's not too ideal since they are different events.

 

Sorry and Thanks!

Update:

I might be able to shave the start and end off the input, since it seems to be of standard length. I'll let you know how it goes, since your method is still the best i've seen.

 

Update 2: Seems like that works after all. I had to cut preceding parts off, and remove the whitespace.

 

My apologies for the rapid editing and u-turns. Thank you very much for your help.

UTC/GMT (Except during BST)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×