Jump to content

Python if input is "ls" then print

WillLTT
Go to solution Solved by lewdicrous,

@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.

Im using Pycharm, how do make something like this:

if input (ls)

then
print("works")

else
print ("error: invalid syntax")

thats not actually code but just a concept,

is it possible todo something like it?

Link to comment
Share on other sites

Link to post
Share on other sites

Use input as per the documentation.

 

I would show you the full code but this smells like homework and it only takes a few minutes to find and read the relevant docs or look up a basic tutorial.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

Use input as per the documentation.

 

I would show you the full code but this smells like homework and it only takes a few minutes to find and read the relevant docs or look up a basic tutorial.

homework? its sunday... in norway there is no friday homework so no, but please link me to those spicy docs

Ive googled for an hour, but nothing seems to work

 

Link to comment
Share on other sites

Link to post
Share on other sites

What is 'ls' meant to be?

If 'ls' is a certain word, then it's relatively easy to solve, but I won't solve the homework for you (if it is homework)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, lewdicrous said:

What is 'ls' meant to be?

If 'ls' is a certain word, then it's relatively easy to solve, but I won't solve the homework for you (if it is homework)

it isnt homework..

if you write ls into the input then, run an certain thing,

im trying to build a thing like a command line

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ughiwanthackintosh said:

homework? its sunday... in norway there is no friday homework so no, but please link me to those spicy docs

Ive googled for an hour, but nothing seems to work

 

So where are you at with the code now? Seems like at least getting input should be easy, comparing strings is quite a simple thing too.. And else is just.. else.

A big part of programming, is debugging. So start introducing breaks in your code (or simple print statements) to see the value of your variables and to see where your code is actually going (e.g. 'is it going into the if or else' type stuff).

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Sauron said:

Use input as per the documentation.

 

I would show you the full code but this smells like homework and it only takes a few minutes to find and read the relevant docs or look up a basic tutorial.

also your input link isnt working ngnix

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, ughiwanthackintosh said:

it isnt homework..

if you write ls into the input then, run an certain thing,

im trying to build a thing like a command line

What have you tried to do so far?

I got it to work in 5 lines.

 

Something like this?

image.png.91342dddab8c099b4b4e0e3f579a4f95.png

image.png.eb6862ad5d322ae08d7854b7a5ffe141.png

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Minibois said:

So where are you at with the code now? Seems like at least getting input should be easy, comparing strings is quite a simple thing too.. And else is just.. else.

A big part of programming, is debugging. So start introducing breaks in your code (or simple print statements) to see the value of your variables and to see where your code is actually going (e.g. 'is it going into the if or else' type stuff).

if ls was typed. then run a command if ls wasnt typed, wait for another command or ls

im trying to make a "command line" like script

Link to comment
Share on other sites

Link to post
Share on other sites

ive tried and gotten this code to work,

start_over = 1

question = input("Do you wish to try again? y/n: ")
if question == "y":
    start_over -= 1
else:
    raise SystemExit

also im alot of a starter so sorry if i am being stupid

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, lewdicrous said:

What have you tried to do so far?

I got it to work in 5 lines

... well idk how you did that but please learn me

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, lewdicrous said:

What have you tried to do so far?

I got it to work in 5 lines.

 

Something like this?

image.png.91342dddab8c099b4b4e0e3f579a4f95.png

image.png.eb6862ad5d322ae08d7854b7a5ffe141.png

yes something like that but with multiple inputs, like

if ls then print something if input is clear then print something else and then if input is version then print something else again and on and on

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, ughiwanthackintosh said:

homework? its sunday... in norway there is no friday homework so no, but please link me to those spicy docs

Ive googled for an hour, but nothing seems to work

 

1 minute ago, ughiwanthackintosh said:

also your input link isnt working ngnix

Right, sorry about that, it's fixed now. You could also find it by googling "python input".

 

If it's not homework you could try something like this

if input() == "ls":
  print("works")
else:
  print("doesn't work")

as for where the docs are, there's literally a link to them on the python homepage and I strongly doubt you "googled for an hour" and couldn't find them, it's the first result for "python documentation".

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, ughiwanthackintosh said:

if ls was typed. then run a command if ls wasnt typed, wait for another command or ls

im trying to make a "command line" like script

Yea I understood that, but what code do you have that is not working now?

14 minutes ago, ughiwanthackintosh said:

Ive googled for an hour, but nothing seems to work

Like where are you at right now after that hour of Googling? Can you receive user input?

Is Python the same as Pycharm?

5 minutes ago, ughiwanthackintosh said:

ive tried and gotten this code to work,


start_over = 1

question = input("Do you wish to try again? y/n: ")
if question == "y":
    start_over -= 1
else:
    raise SystemExit

also im alot of a starter so sorry if i am being stupid

From what I read here:

https://www.pythonforbeginners.com/basics/getting-user-input-from-the-keyboard

It looks like 'input' reads integers (numbers with no decimal points), so try using 'raw_input' there.

EDIT: nvm, that's wrong it seems..

Also, I am fairly sure you need to use a single apostrophe when comparing two strings. So that means a single apostrophe around the 'y'.

 

It might be better at this point to also include a simple 'print' statement on the if and else to see how far your code gets. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sauron said:

 

Right, sorry about that, it's fixed now. You could also find it by googling "python input".

 

If it's not homework you could try something like this


if input() == "ls":
  print("works")
else:
  print("doesn't work")

as for where the docs are, there's literally a link to them on the python homepage and I strongly doubt you "googled for an hour" and couldn't find them, it's the first result for "python documentation".

i didnt google for the docs. but i googled "python if input is then" and "if python" and simmilar things

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, ughiwanthackintosh said:

... well idk how you did that but please learn me

# input a string
x = str(input("Enter a word: "))
# if input is correct, print
if x == "ls": 
  print("Works")
elif x == "as":
  print("Works")
elif x == "bn":
  print("Works")
# if input is wrong, print
else: 
  print("error: invalid syntax")

Something like this maybe.

An if statement for every input you want, although the code could get long.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Minibois said:

Yea I understood that, but what code do you have that is not working now?

Like where are you at right now after that hour of Googling? Can you receive user input?

Is Python the same as Pycharm?

From what I read here:

https://www.pythonforbeginners.com/basics/getting-user-input-from-the-keyboard

It looks like 'input' reads integers (numbers with no decimal points), so try using 'raw_input' there.

Also, I am fairly sure you need to use a single apostrophe when comparing two strings. So that means a single apostrophe around the 'y'.

 

It might be better at this point to also include a simple 'print' statement on the if and else to see how far your code gets. 

i am at this point after googling:

print ("Version 3.2.")
print ("Type Help for list of commands.")

input1 = input (">")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")
result = print (" ")

input2 = input ("")
input3 = input (">")

thats when i goto the part of multiple inputs and if input isnt clear then either invalid syntax, or if its ls then run some else code

9 minutes ago, Minibois said:

Is Python the same as Pycharm?

yes pycharm is a ide editor and executor of python

 

10 minutes ago, Minibois said:

rom what I read here:

https://www.pythonforbeginners.com/basics/getting-user-input-from-the-keyboard

It looks like 'input' reads integers (numbers with no decimal points), so try using 'raw_input' there.

Also, I am fairly sure you need to use a single apostrophe when comparing two strings. So that means a single apostrophe around the 'y'.

 

It might be better at this point to also include a simple 'print' statement on the if and else to see how far your code gets. 

could try that yes

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, lewdicrous said:

# input a string/command
x = str(input("Enter a word: "))
# if input is correct, print
if x == "ls": 
  print("Works")
elif x == "as":
  print("Works")
elif x == "bn":
  print("Works")
# if input is wrong, print
else: 
  print("error: invalid syntax")

Something like this maybe.

An if statement for every input you want, although the code could get long.

its almost perfect! but when the input is done running then loop and go back to start to run more commands

 

also where did you find a guide for this? in the docs? or ?

 

also a sidenote question,

the py script works in pycharm (google it)

but not  if i drag it into python interpreter do i need to execute a certaint command to run a py in the interpreter?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, ughiwanthackintosh said:

its almost perfect! but if invalid syntax then goto top and run over again

If that's the case, then you might need to put the if statements in a while loop.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, lewdicrous said:

If that's the case, then you might need to put the if statements in a while loop.

ill google that

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, lewdicrous said:

If that's the case, then you might need to put the if statements in a while loop.

i googled it for 3 minutes now i found this:

i = 1
while i < 6:
  print(i)
  i += 1

unfortunatly im not wise enought to know how to use it or where to put it or if its even the right while loop

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, ughiwanthackintosh said:

ill google that

Just beware of infinite loops.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

Just beware of infinite loops.

 

1 minute ago, ughiwanthackintosh said:

i googled it for 3 minutes now i found this:


i = 1
while i < 6:
  print(i)
  i += 1

unfortunatly im not wise enought to know how to use it or where to put it or if its even the right while loop

ill google some more while waiting for a reply in the one above

 

thanks for sticking around with me

i am really bad a python but i gotta start somewhere right??

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, ughiwanthackintosh said:

 

ill google some more while waiting for a reply in the one above

 

thanks for sticking around with me

i am really bad a python but i gotta start somewhere right??

found this

while <expr>:
    <statement(s)>

same problem as before i cant understand where to put it

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, ughiwanthackintosh said:

i googled it for 3 minutes now i found this:


i = 1
while i < 6:
  print(i)
  i += 1

unfortunatly im not wise enought to know how to use it or where to put it or if its even the right while loop

That code means: while i is less than 6, print i, then add 1 to i.

i += 1 means i = i + 1.

You gotta find a way to incorporate that while loop into the code you want to make, but not necessarily the way the code you linked shows.

8 minutes ago, ughiwanthackintosh said:

i am really bad a python but i gotta start somewhere right??

Have you done any basics? that a good place to start before you start making your own projects.

This might help a bit.

 

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

×