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.

1 minute ago, lewdicrous said:

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

i += 1 means i = i + 1.

.. still dont understand a thing

 

1 minute ago, lewdicrous said:

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

This might help a bit.

 

nope havent i just jumped onto hello world and calc and im here now

will watch!

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ughiwanthackintosh said:

.. still dont understand a thing

i = 1 # i can be any number in this example
while i < 6: # check if i is less than 6
  print(i) # print i
  i += 1 # add 1 to i then check again
  # when i is equal to 6, the loop stops
  
# here's another example
y = 10 # in this case, y is equal to 10
while y > 0: # check if y is greater than 0
  print(y) # print y
  y -= 1 # subtract 1 from y then check again
  #when y is squal to 0, the loop stops

Is this a better explanation?

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, lewdicrous said:

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.

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

This might help a bit.

 

 

i posted this secounds after you posted above me

 

 

 

 

 

before you leave me in the infinite hell of guides and years of trubleshooting can you provide a working while loop?

i understand this:

while (expr) # waiting for break

 

break  # pauses

 

continue # continues from break

 

but how do i make it go back up to

37 minutes ago, lewdicrous said:

# 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

2 minutes ago, ughiwanthackintosh said:

can you provide a working while loop?

The code I tried didn't work.. trying to debug it now lol

I haven't been coding for over a year, so I'm a bit rusty.

I've also only taken one course on it, so I'm by no means an expert.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, lewdicrous said:

i = 1 # i can be any number in this example
while i < 6: # check if i is less than 6
  print(i) # print i
  i += 1 # add 1 to i then check again
  # when i is equal to 6, the loop stops
  
# here's another example
y = 10 # in this case, y is equal to 10
while y > 0: # check if y is greater than 0
  print(y) # print y
  y -= 1 # subtract 1 from y then check again
  #when y is squal to 0, the loop stops

Is this a better explanation?

i understand what your doing in that code atleast somewhat

but i dont get where to put the other code

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

The code I tried didn't work.. trying to debug it now lol

I haven't been coding for over a year, so I'm a bit rusty.

I've also only taken one course on it, so I'm by no means an expert.

your better than me obv thanks for helping :D

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, lewdicrous said:

The code I tried didn't work.. trying to debug it now lol

I haven't been coding for over a year, so I'm a bit rusty.

I've also only taken one course on it, so I'm by no means an expert.

dont need to be an expert :D

i thank for any help :D

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ughiwanthackintosh said:

i understand what your doing in that code atleast somewhat

but i dont get where to put the other code

That's just an example of a while loop, you'll need to change it if you want your code to work the way you want.

Those examples are just counters, if you will.

This is the output of the first example:

image.png.75fe6695a29424314a3714d45491dfb6.png

This is the output of the second example:

image.png.654af20dca8961e899d2dbb0c685412c.png

 

You can use this site https://repl.it/languages/python3 to test codes quickly.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, lewdicrous said:

That's just an example of a while loop, you'll need to change it if you want your code to work the way you want.

Those examples are just counters, if you will.

This is the output of the first example:

image.png.75fe6695a29424314a3714d45491dfb6.png

This is the output of the second example:

image.png.654af20dca8961e899d2dbb0c685412c.png

 

You can use this site https://repl.it/languages/python3 to test codes quickly.

im using Pycharm for windows, but if i need an alternative ill use repl it

pycharm

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, lewdicrous said:

That's just an example of a while loop, you'll need to change it if you want your code to work the way you want.

Those examples are just counters, if you will.

This is the output of the first example:

image.png.75fe6695a29424314a3714d45491dfb6.png

This is the output of the second example:

image.png.654af20dca8961e899d2dbb0c685412c.png

 

You can use this site https://repl.it/languages/python3 to test codes quickly.

the problem is idk how i should change it to make it work my my current code...

im not that good yet, but you can try to explain if you want

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ughiwanthackintosh said:

im using Pycharm for windows, but if i need an alternative ill use repl it

pycharm

Repl is an online compiler, IDE and not something you have to download.

You can still use pycharm, but if you want to test a simple code like the ones I explained, then you can use Repl for those.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

Repl is an online compiler, IDE and not something you have to download.

You can still use pycharm, but if you want to test a simple code like the ones I explained, then you can use Repl for those.

mhm!

however where do i place the while loop in the code?

also you never posted your debug'ed code

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, ughiwanthackintosh said:

the problem is idk how i should change it to make it work my my current code...

im not that good yet, but you can try to explain if you want

I think what you're looking to do is use conditions and loops, but I'm not sure about how to incorporate it in the way you want.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, lewdicrous said:

I think what you're looking to do is use conditions and loops, but I'm not sure about how to incorporate it in the way you want.

isnt there like a thing like this???

:1

code code
code code
code code

goto :1

like in batch scripts, there was exactly that. like

:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :y
if /I "%c%" EQU "N" goto :n
goto :choice


:y

echo "y"
pause
exit

:n

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, ughiwanthackintosh said:

i never said -snip-

We write - snip - to shorten the quote.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, lewdicrous said:

We write - snip - to shorten the quote.

oh! so id get the a notification! brilliant

class label(Exception): pass  

try:
    ...
    if condition: raise label()  
    ...
except label:  
    pass
...

this code does not work, i tested it

i did change the label to goto top

pic listed below does not work much better

Capture.PNG

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, ughiwanthackintosh said:

- snip -

That code was just an example, it will not work out of the box; you gotta make it work for what you want.

Search for "python goto substitute"

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

That code was just an example, it will not work out of the box; you gotta make it work for what you want.

Search for "python goto substitute"

do you know how i can make the code work ? beacuse googling "python goto substitute"

opens an infinite hell of trubleshooting

Link to comment
Share on other sites

Link to post
Share on other sites

I think this is what you're trying to do, but I'm not sure about how to get it to work.

image.png.6d0ef76545778791671df68dc75232ca.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

I think this is what you're trying to do, but I'm not sure about how to get it to work.

image.png.6d0ef76545778791671df68dc75232ca.png

it is i think.

should i ask other forums?

on the topic? or? maybe someone there knows how? :)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, ughiwanthackintosh said:

it is i think.

should i ask other forums?

on the topic? or? maybe someone there knows how? :)

Up to you, you might find an answer in stack overflow or some subreddit, but you might also find the answer on YouTube.

Best of luck to you.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, lewdicrous said:

Up to you, you might find an answer in stack overflow or some subreddit, but you might also find the answer on YouTube.

Best of luck to you.

thanks. ? im gona ask the python discord server

Link to comment
Share on other sites

Link to post
Share on other sites

@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

 

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

×