Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
ughiwanthackintosh

Python if input is "ls" then print

@ughiwanthackintosh Do you mean something like this? Or something else?

Code 1:

Spoiler

while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "as": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "bn": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break 
  else: #if input is not similar
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue

 

If so, you can replace "break" with "continue" if you wanted the user to stay in the loop, but you'll need to add another if statement for them to exit the loop.

 

Edit: this loop works somewhat similarly to what you provided earlier, with additional commands and an if statement for exiting the loop.

Code 2:

Spoiler

while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is equal to "ls"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "as": #checks if input is equal to "as"
    print("Works")
    continue #returns to start of loop
  elif user_input == "bn": #checks if input is equal to "bn"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "exit": #checks if input is equal to "exit"
    print("Exiting")
    break #exits loop
  else: #if input is not available
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue #returns to start of loop

 

Difference between code 1 and code 2: code 1 exits the loop if the input is correct/available, while code 2 stays in the loop if the input is correct/available.

Recommended Posts

Posted · Original PosterOP
14 hours ago, lewdicrous said:

@ughiwanthackintosh Try this out.


while True:
  user_input = str(input("Enter a word: ")) # the word/letters the user has to input
  known_commands = ("ls", "as", "bn") # list of words/letters to make the code shorter and more compact
  if user_input in known_commands: # checks if user_input matches something in known_commands
    print("Works") # if so, prints
    break # exits the loop
  else:
    print("Error: invalid syntax") # if not, prints
  continue # returns to the start of the loop

 

i managed to get it to work

print ("SResource 3.1. Built By William Ingebrigtsen.")
print ("Type Help for list of commands.")

while True:
    x = input("SR: ")

    if x == "ls":
        print("py.py")
    elif x == "clear":
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
        print(" ")
    elif x == "exit":
        exit()

    else:
        print("error: invalid syntax")

 

Link to post
Share on other sites
Posted · Original PosterOP
On 9/2/2019 at 6:46 AM, lewdicrous said:

Whichever works best for you.

holup, this version of the code is alot more clean and usefull, but can i assign 1 command to every input?

Link to post
Share on other sites
4 minutes ago, ughiwanthackintosh said:

can i assign 1 command to every input?

Not sure I understand what you mean, please elaborate.

Link to post
Share on other sites
Posted · Best Answer

@ughiwanthackintosh Do you mean something like this? Or something else?

Code 1:

Spoiler

while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "as": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "bn": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break 
  else: #if input is not similar
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue

 

If so, you can replace "break" with "continue" if you wanted the user to stay in the loop, but you'll need to add another if statement for them to exit the loop.

 

Edit: this loop works somewhat similarly to what you provided earlier, with additional commands and an if statement for exiting the loop.

Code 2:

Spoiler

while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is equal to "ls"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "as": #checks if input is equal to "as"
    print("Works")
    continue #returns to start of loop
  elif user_input == "bn": #checks if input is equal to "bn"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "exit": #checks if input is equal to "exit"
    print("Exiting")
    break #exits loop
  else: #if input is not available
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue #returns to start of loop

 

Difference between code 1 and code 2: code 1 exits the loop if the input is correct/available, while code 2 stays in the loop if the input is correct/available.

Link to post
Share on other sites
42 minutes ago, ughiwanthackintosh said:

holup, this version of the code is alot more clean and usefull, but can i assign 1 command to every input?

You can do whatever you want. Add more if statements for each command you want to add.

 

While you're doing this, I highly recommend reading a book or watching some introductory guides, as you said you know basically nothing about programming at the moment. Take a look at Learn Python the Hard Way, it's covers a lot from things like how to run python scripts to more advanced stuff like classes.

 

Another tip: get rid of the IDE for now and go text editor + powershell/terminal. That way you'll understand much better how things work and what the IDE does for you, behind the scenes.


Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | Storage: 250 GB Crucial BX100 SSD + 2 TB Seagate HDD + 1TB WD Green + 3TB WD Red | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9360 | CPU: i5 7200U | RAM: 8 GB

Link to post
Share on other sites

I think this would be the cleanest solution:

def ls():
  print("Called ls")
  
def clear():
  print("\n" * 100)
  
while True:
  user_input = str(input("Command: "))
  if user_input in locals():
    locals()[user_input]()
  else:
    print("ERROR: Unknown command {}".format(user_input))

For added control, replace the if statement with a defined list of valid commands as in previous examples, as opposed to locals().

Link to post
Share on other sites
Posted · Original PosterOP
On 9/5/2019 at 10:09 AM, lewdicrous said:

@ughiwanthackintosh Do you mean something like this? Or something else?

Code 1:

  Reveal hidden contents


while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "as": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break
  elif user_input == "bn": #checks if input is similar to assigned command
    print("Works") #prints message then exits loop
    break 
  else: #if input is not similar
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue

 

If so, you can replace "break" with "continue" if you wanted the user to stay in the loop, but you'll need to add another if statement for them to exit the loop.

 

Edit: this loop works somewhat similarly to what you provided earlier, with a additional commands and an if statement for exiting the loop.

Code 2:

  Reveal hidden contents


while True:
  user_input = str(input("Enter a word: ")) #input from user
  if user_input == "ls": #checks if input is equal to "ls"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "as": #checks if input is equal to "as"
    print("Works")
    continue #returns to start of loop
  elif user_input == "bn": #checks if input is equal to "bn"
    print("Works") 
    continue #returns to start of loop
  elif user_input == "exit": #checks if input is equal to "exit"
    print("Exiting")
    break #exits loop
  else: #if input is not available
    print("Error: invalid syntax") #prints message then returns to start of loop
  continue #returns to start of loop

 

Difference between code 1 and code 2: code 1 exits the loop if the input is correct/available, while code 2 stays in the loop if the input is correct/available.

code 2 is correct! thanks!

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

Buy VPN

×