Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i make python look for a specific word (CLOSED)

So i have this code:

x = 1
while x <= 2:
    text = input("> ")
    to_deny = "!?/"
    find=["find subsystem"]
    if any(char in text for char in to_deny):
        print("text contains restricted characters")
        quit()
    if any(char in text for char in find):
        print(f"finding subsystem: {text}")
    if text == "quit":
        quit()
    elif text == "clear":
       import os
       os.system("clear")
    else:
       print(f"running {text}....")

And i want to make it so when you say " find subsystem" (cringe, i know) it will instead print "finding subsystem: (name of subsystem goes here)" and not "finding subsystem: (subsystem) running (subsystem)…."
I have tried everything i could possibly think of but my stupid 2 brain cell mind only tried:
Using lists (i forgot how to make one :/)
Using a dictionary (forgot how to make one too)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Put all the mutually exclusive operations that don’t result in a break or continue in a single if...elif...else chain so that you don’t "fall through" from one into the next.

import os

to_deny = "!?/"
find = "find subsystem "
while True:
    text = input("> ")
    if any(char in text for char in to_deny):
        print("text contains restricted characters")
        break
    if text == "quit":
        break

    if find in text:
        text = text.replace(find, "")
        print(f"finding subsystem: {text}")
    elif text == "clear":
        os.system("clear")
    else:
        print(f"running {text}....")
> find subsystem abc
finding subsystem: abc
> blargh
running blargh....
>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading