-2

I want to take unknown number of positive and negative integer number from user. The input will stop when the user press the Enter key.

For example - If an user enter 1 2 3 4 5 -9 -10 1000 -Enter Key-
then it will store "1 2 3 4 5 -9 -10 1000"

Here is my tried code -

a = []
inp = input()

while inp != "\n":
    a.append(inp)
    inp = input()

print(a)

But this is not stop taking input even after pressing Enter Key.

Edit - This question asked for taking input until empty input but my question is taking input in one line until user press Enter button.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
mefahimrahman
  • 197
  • 2
  • 6
  • 29
  • Does this answer your question? [Accepting input till newline in python](https://stackoverflow.com/questions/20511159/accepting-input-till-newline-in-python) – Pranav Hosangadi Oct 29 '20 at 20:31
  • 1
    _" my question is taking input in one line until user press Enter button."_ That is how the `input()` function works straight out of the box. You don't need any loops. Once you have the input, `split()` it on whitespaces to get a list of the actual inputs – Pranav Hosangadi Oct 29 '20 at 20:35
  • But I do not know how many numbers the user going to input. The number of input can vary. – mefahimrahman Oct 29 '20 at 20:37
  • 1
    Try `print("1 2 3 4 5 -9 -10 1000".split())` and see for yourself. – Matthias Oct 29 '20 at 20:39
  • 1
    Run this code: `x = input('Gimme some numbers: ')`. See how it works. Try to enter 2 numbers. Then try to enter 10. Does it work in both cases? What's the value of `x`? [Read the documentation for the `input()` function](https://docs.python.org/3/library/functions.html#input). – Pranav Hosangadi Oct 29 '20 at 20:39
  • Yes. My bad. It was a very silly question. But thank you all for your time. – mefahimrahman Oct 29 '20 at 20:46

2 Answers2

1

Just change it to

while inp:
    a.append(inp)
    inp = input()

When newline will be entered, inp is an empty string, which is falsy, thus breaking the loop.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
  • `inp` won't become `None`. [`input`](https://docs.python.org/3/library/functions.html#input) always returns a string (if it doesn't throw an exception), so `inp` will be an empty string. – Matthias Oct 29 '20 at 20:37
  • @AbhinavMathur judging by their comment on the other answer, OP also wanted it appended to a list. IDK why they didn't think of just adding an `.append()` line to your code and instead claimed it doesn't work. :-/ – Pranav Hosangadi Oct 29 '20 at 20:44
  • @PranavHosangadi I added the append statement, what is missing then? – Abhinav Mathur Oct 29 '20 at 20:46
  • 1
    @AbhinavMathur _"what is missing then"_ nothing! That's my point. Even before you added the append, nothing was "missing" because 1. OP didn't specify that they wanted them appended to a list, and 2. that's a fairly trivial thing to identify and add anyway – Pranav Hosangadi Oct 29 '20 at 20:48
  • @PranavHosangadi the "accepted" answer would work only on specific versions of python :/ – Abhinav Mathur Oct 29 '20 at 20:51
  • Don't worry about reputation and answers being accepted! It's all for fake internet points anyway. Your answer will be read by people in the future and if it helps them, that's what Stack Overflow is all about. FWIW, I upvoted your answer. – Pranav Hosangadi Oct 29 '20 at 20:53
  • Not talking about the points honestly, just comparing the acceptability of solutions in general – Abhinav Mathur Oct 29 '20 at 20:56
1

If you are using Python 3.8, you could utilise the walrus operator here

ls = []

while (inp := input("> ")):
    ls.append(inp)

print(ls)
Joshua Nixon
  • 1,379
  • 2
  • 12
  • 25