-2

I am trying to type the following into the IDLE:

userInput = input('Enter 1 or 2: ')
if userInput == "1": print ("Hello World") print (“How
are you?”) elif userInput == "2": print ("Python
Rocks!") print (“I love Python”) else:
print ("You did not enter a valid number")

However, the moment after I typed the first line and pressed Enter, the program runs and asks me to Enter 1 or 2.

How can I write the full set of instructions before running?

Thanks

I've figured it out. It should be as follows:

userInput = input('Enter 1 or 2: ')
if userInput == "1":
    print ("Hello World")
    print ("How are you?")
elif userInput == "2":
    print ("Python Rocks!")
    print ("I love Python")
else: print ("You did not enter a valid number")

It seems I wrote it originally in the Shell and NOT the IDLE.

dugdugdug
  • 19
  • 1
  • 3
  • You can use `;` and write all instructions in single line – Olvin Roght Aug 26 '20 at 15:59
  • 1
    Why do you have everything smooshed together instead of writing over multiple lines? Also, are you running this in a REPL? Python doesn't just run code as it's being written unless you're writing in an interactive console. If you are, the answer then is to write in a text file and run it manually when you want it to run. – Carcigenicate Aug 26 '20 at 15:59

2 Answers2

0

You can make it functions:

def actOnInput(userInput):
  if userInput == "1":
    print ("Hello World")
    print ("How are you?")
  elif userInput == "2":
    print ("Python Rocks!")
    print ("I love Python")
  else:
    print ("You did not enter a valid number")

def askForInputAndActOnIt():
  userInput = input('Enter 1 or 2: ')
  actOnInput(userInput)

askForInputAndActOnIt()

In a python REPL it will look something like this:

>>> def actOnInput(userInput):
...   if userInput == "1":
...     print ("Hello World")
...     print ("How are you?")
...   elif userInput == "2":
...     print ("Python Rocks!")
...     print ("I love Python")
...   else:
...     print ("You did not enter a valid number")
... 
>>> def askForInputAndActOnIt():
...   userInput = input('Enter 1 or 2: ')
...   actOnInput(userInput)
... 
>>> askForInputAndActOnIt()
Enter 1 or 2: 2
Python Rocks!
I love Python
>>> askForInputAndActOnIt()
Enter 1 or 2: 1
Hello World
How are you?
>>>
Stef
  • 13,242
  • 2
  • 17
  • 28
0
  1. Open IDLE

  2. Go to Options > Configure IDLE > General

  3. Make sure you select Open Edit Window Radio button

  4. Click Apply and OK

    Close IDLE and open it again

Schäfer
  • 181
  • 1
  • 1
  • 7