27

I am trying to define a function to make the perimeter of a rectangle. Here is the code:

width = input()
height = input()
def rectanglePerimeter(width, height):
   return ((width + height)*2)
print(rectanglePerimeter(width, height))

I think I haven't left any arguments opened or anything like that.

Chimp
  • 661
  • 2
  • 6
  • 10
  • 1
    And what do you enter when `input()` asks you for input? Are you running this in a terminal or in an IDE? Note that `input()` returns a string, not an `int` so your calculation will fail. – Martijn Pieters Jul 16 '13 at 11:57
  • related: [Python 3: EOF when reading a line (Sublime Text 2 is angry)](http://stackoverflow.com/q/12547683/4279) – jfs Jul 16 '13 at 12:19
  • What's your question? To start, do you understand what that error message means? Next, how are you running your code? IDE, terminal, online testing site... Please [edit] to clarify. See [ask]. – wjandrea Nov 25 '22 at 20:33
  • To be clear, if you run your code in a system that doesn't provide any input on stdin, or not enough lines, you'll get that error. For a minimal example in Bash: `printf '' | python3 -c 'input()'` – wjandrea Nov 26 '22 at 01:01

3 Answers3

19
width, height = map(int, input().split())
def rectanglePerimeter(width, height):
   return ((width + height)*2)
print(rectanglePerimeter(width, height))

Running it like this produces:

% echo "1 2" | test.py
6

I suspect IDLE is simply passing a single string to your script. The first input() is slurping the entire string. Notice what happens if you put some print statements in after the calls to input():

width = input()
print(width)
height = input()
print(height)

Running echo "1 2" | test.py produces

1 2
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 5, in <module>
    height = input()
EOFError: EOF when reading a line

Notice the first print statement prints the entire string '1 2'. The second call to input() raises the EOFError (end-of-file error).

So a simple pipe such as the one I used only allows you to pass one string. Thus you can only call input() once. You must then process this string, split it on whitespace, and convert the string fragments to ints yourself. That is what

width, height = map(int, input().split())

does.

Note, there are other ways to pass input to your program. If you had run test.py in a terminal, then you could have typed 1 and 2 separately with no problem. Or, you could have written a program with pexpect to simulate a terminal, passing 1 and 2 programmatically. Or, you could use argparse to pass arguments on the command line, allowing you to call your program with

test.py 1 2
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
9

Use a try / except block to get rid of the EOF error.

try:
    width = input()
    height = input()
    def rectanglePerimeter(width, height):
       return ((width + height)*2)
    print(rectanglePerimeter(width, height))
except EOFError as e:
    print(end="")
tripleee
  • 175,061
  • 34
  • 275
  • 318
Saurabh Raj
  • 107
  • 1
  • 4
  • 1
    This makes it so the script doesn't do anything if it hits that error. How is that helpful? And why on Earth would you put the `def` in the `try` block? It should be above it if anywhere. – wjandrea Nov 25 '22 at 20:36
3

convert your inputs to ints:

width = int(input())
height = int(input())
astrognocci
  • 1,057
  • 7
  • 16
  • Depends on whether this is Python 2 or 3. Remember that `print()` can work in python 2 (although it probably is 3) – TerryA Jul 16 '13 at 11:59
  • yes, but the question is tagged with `python-3.x, so i'm assuming py3 ;) – astrognocci Jul 16 '13 at 12:00
  • 1
    Ok I tried the int() thing but it still came up with that same error. I am entering numbers in and using IDLE 3. – Chimp Jul 16 '13 at 12:00
  • @astrognocci WOW, I did not see that. Epic fail by me :p – TerryA Jul 16 '13 at 12:00
  • Are you running the program directly from idle (pressing F5) ? Apparently some terminals can't handle input correctly. I don't remember IDLE having trouble with it, but i haven't used it in a while and don't have it here, so maybe that's it – astrognocci Jul 16 '13 at 12:06