10

How do i write

   >>> x = int(raw_input("Please enter an integer: "))
    >>> if x < 0:
    ...      x = 0
    ...      print 'Negative changed to zero'
    ... elif x == 0:
    ...      print 'Zero'
    ... elif x == 1:
    ...      print 'Single'
    ... else:
    ...      print 'More'
    ...

this in IDLE. As soon as I hit enter after writting first line, it executes the first line and i am not able to write full code. I am very new to python, just started it today. Any help will be appreciated.

Himanshu.MarJAVA
  • 475
  • 1
  • 11
  • 33
  • 1
    You don't have to execute it all in one go, if you do, then use a file for multiple runs or declare a function for a single run. Even if it executes earlier than you expected, you'll still have the entered value in the `x` variable. – KurzedMetal May 02 '12 at 12:26

7 Answers7

13

Try File => New File in top menu. Then write your code in this windows and run it by F5 key (or Run in top menu)

Aligus
  • 1,585
  • 1
  • 9
  • 11
9

Shift + Enter takes you to next line without executing the current line.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Jayanth Reddy
  • 347
  • 1
  • 3
  • 7
8

1: Use semicolons between lines
2: Try iPython
3: Write it as a function, e.g.

def myfunc():
    x = int(raw_input("Please enter an integer: "))
    if x < 0:
        x = 0
        print 'Negative changed to zero'
    elif x == 0:print 'Zero'
    elif x == 1:print 'Single'
    else:print 'More' 
Jay M
  • 3,736
  • 1
  • 24
  • 33
3

Using the exec function along with multi-line strings (""") worked well for my particular use case:

exec("""for foo in bar:
  try:
    something()
  except:
    print('Failed')"""
joe
  • 3,752
  • 1
  • 32
  • 41
2

Can't write multi-line code in python console. Need a 3rd-party app.

Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • Leaving my prior misconception because nobody corrected me and it has been 8 years. Maybe it is correct and the new info I have is not correct? To extend the statement to one or more lines we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation character slash “\”. - https://www.geeksforgeeks.org/python-multi-line-statements/ – Wolfpack'08 Jan 24 '23 at 18:57
2

If you do File --> New File, it should open a new savable window that you can write multiple lines and save as a .py file.

0

creating a new file enables you to write your code in multiline in IDLE and before writing the code you need to save the file as *.py format. That's all