0

I want to execute the following program called APlusB.py in my OSX terminal, enter two numbers for the inputs and have it compute the values and exit. In my terminal I type:

$ python3 APlusB.py

then I get a little cursor on a blank line, I type

3 4

what do I do after that? If I hit Ctl + d then the program terminates, which is what I want, but it prints 7D and I would prefer if it would just compute my value, and print out 7

# Uses python3
import sys
input = sys.stdin.read()
tokens = input.split()
a = int(tokens[0])
b = int(tokens[1])
print(a + b)

Thank you for your help.

6
  • 1
    Have you tried pressing the return key? Commented Mar 23, 2016 at 6:04
  • yes - I just get more blank lines, the program doesn't exit back to the shell Commented Mar 23, 2016 at 6:05
  • BTW, you probably get 7D (or, more likely, 7^D) because the program outputs 7 and you typed Ctrl+D. Terminals handle codes of the form Ctrl+Key by signaling the reader program, usually a shell or a program invoked directly or indirectly be the shell, about that. Libraries such as curses can handle stuff so that nothing gets printed and the program can handle the key combo in some way. However, a simple program wont do that, and the terminal will thus echo ^Key by default. For example, most shells, when detecting ^C (a.k.a Ctrl+C), will send SIGINT to the foreground task. Commented Mar 23, 2016 at 6:11
  • thank you KemyLand, that makes sense, I wasn't sure how to make the program show me my result and exit properly. Commented Mar 23, 2016 at 6:15
  • @GregBailey: BTW, the output of the program does not contain D. That's just your terminal mixing up the program's output and other things. If you take your program's output and use it as input for another program, the "D" won't be there. Commented Mar 23, 2016 at 6:24

4 Answers 4

3

sys.stdin.read waits for the user to enter EOF.

Try using input it will return when the user enters a new line. Don't name your variable input as you'll be redefining the input function you'll need to use.

Sign up to request clarification or add additional context in comments.

3 Comments

this code is a sample from a class, and I have zero python experience. In the video, the instructor does some key command to have it just print the number and exit. Isn't Ctl+D EOF, and then shouldn't it just print 7 and not 7D?
that definitely works, but I wonder how to send EOF in such a way that you get 7 and not 7D, thank you for your answer.
@GregBailey: There's no way in "interactive" mode, so to call it (though it's not). See my comment in the question for details. However, you can use pipes to do that, just like this: echo -e '3\n7' | python3 ./APlusB.py.
1

Please use sys.stdin.readline()

stdin.read(1) reads one character from stdin. If there was more than one character to be read at that point (e.g. the newline that followed the one character that was read in) then that character or characters will still be in the buffer waiting for the next read() or readline().

import sys
input = sys.stdin.readline()
tokens = input.split()
a = int(tokens[0])
b = int(tokens[1])
print(a + b)

the usage:

esekilvxen263 [7:05] [/home/elqstux] -> python wy.py
3 4
7
esekilvxen263 [7:06] [/home/elqstux] -> 

1 Comment

You should probably replace your customized shell prompt with $, for clarity's sake.
0
import sys
input = sys.stdin.read()
tokens = input.split()
a = int(tokens[0])
b = int(tokens[1])
print tokens
print(a + b)

If you put print tokens front print(a+b).It works as you expect.

1 Comment

I tried this, (had to change it to print(tokens) and then it prints out the tokens array and the product without the D when pressing ctl+D. Interesting. Why in this case does it print out with out the D?
0

This is the best way to take in input. It is intended for Python 3.

tokens = input()
tokens = tokens.split()
a = int(tokens[0])
b = int(tokens[1]) 
print(a + b)

5 Comments

Which version of python?
@KemyLand no, it can't convert 3 4 to an integer.
@PeterWood: Sorry, didn't looked closely. Actually, this doesn't even work for any version of Python...
@Justice with your edit it will now print 34 on Python 3.
@PeterWood: That explains things a little better... Rolled back and edited so that its clear this is for Py3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.