0

I'm here again.I'm newbie and I have some questions.

I have a code like that,

enter = input("Please type something: ")

if enter == "1":
    print("Hello")

else:
    print("Error")

How I keep run the program after else. So How I do when I type something other than 1, it does say Error and again say Please type something. How how how :=)

I know my English is bad and I ask a lot of questions :(

Emek Kırarslan
  • 315
  • 1
  • 4
  • 11

2 Answers2

2

You can wrap your code in an infinite loop, so that is iterated indefinitely:

while True:
    enter = input("Please type something: ")
    if enter == "1":
        print("Hello")
    else:
        print("Error")

Terminate your program by hitting CTRL+C on Linux/MacOS or CTRL+Z on Windows. If you want, you can use a word to terminate the program, like this:

while True:
    enter = input("Please type something: ")
    if enter == "1":
        print("Hello")
    elif enter == "quit":
        break
    else:
        print("Error")

Also, if you are using Python 2, then replace input with raw_input, so that what you type is returned verbatim as a string with the trailing new line stripped. See PEP 3111 for more information.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
0

Stick it in a loop.

enter="0"
while True:
    enter = input("Please type something.")

    if enter == "1":
        print("hello")
    else:
        print("error")

This will keep running until you close the program.

Isaiah Taylor
  • 615
  • 1
  • 4
  • 17