English

We will try to read input from the user. Press Ctrl-d and see what happens: >>> s = raw_input('Enter something --> ')

Advertisements
Advertisements

Question

We will try to read input from the user. Press Ctrl-d and see what happens:

>>> s = raw_input('Enter something --> ')

Code Writing
Advertisements

Solution

In Python 2, pressing Ctrl-D sends an end-of-file signal. Since no input is supplied, raw_input() raises an EOFError exception. It can be handled as follows:

try:
    s = input("Enter something --> ")
except EOFError:
    print("End of input reached.")

In Windows, Ctrl-Z followed by Enter generally performs the equivalent EOF operation.

shaalaa.com
  Is there an error in this question or solution?
Chapter 8: Exception Handling & Generate Functions - EXERCISE [Page 156]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 8 Exception Handling & Generate Functions
EXERCISE | Q 2. | Page 156
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×