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?
CodePudding user response:
Sure you can. Perhaps the easiest way to use the if/elif/else
in your code.
Code:
x = 1
while x <= 2:
text = input("> ")
x = x 0
if text == "quit":
quit()
elif text == "clear":
import os
os.system("clear")
else:
print(f"running {text}....")
Output:
>>> python3 test.py
> foo
running foo....
> quit
CodePudding user response:
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}....")