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

Is there a way for me to add an exception to my code? (CLOSED)

So i have this code:

x=1
while x <= 2:
    text=input("> ")
    print(f"running {text}....")
    x=x+0
    if text=="quit":
        quit()
    if text=="clear":
        import os
        os.system("clear")

So i wanted to know if there was a way for me to tell python "hey could you please not print "running…" on these two words, clear and quit?" And well i tried all i could think of, and it either: ignored it, or it threw up an error telling me to screw off, could you guys maybe help me?

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 :

It’s pretty similar to fizzbuzz. That’s probably why it seems hard to come up with a "nice" solution.

text=input("> ")
if text != "quit" && text != "clear":
    print(f"running {text}....")

I was just about to add an alternative and better approach with elif and else, but @milanbalazs was faster. 🙂

When you expand the application, this approach might be a good option:

nonprintables = ["quit", "clear"]
while x <= 2:
    text=input("> ")
    if not text in printables:
        print(f"running {text}....")
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