-2

I need to enter n multiple integer numbers on the same line in Python without using a list or any other container (set, dict, etc.) (please don't ask me why, let's say I have to, and that's the point of that question).

I know that in C++ you can write something like this:

int how_many_numbers, number;
cin >> how_many_numbers;
for (int i = 0; i < how_many_numbers; i++) {
    cin >> number;
    // do something with it
}

The coolest thing about cin here is that it doesn't give a thing about how you input the numbers: you can input one number and hit the enter button, then you can input 3 numbers on the same line and hit the enter again, than another 2 or 4 and so on until you input all the numbers that you have to.

My question is: Is there anything like cin in Python which will allow me to enter n multiple integer numbers on the same line without using a list or any other container?

I appreciate any help.

alekscooper
  • 795
  • 1
  • 7
  • 19
  • I don't understand what do you want to achieve. If you want to put for example `3 1 2 4` to load `3` numbers: `1`, `2` and `4` why you don't do it with `numbers = input().split(" ")`? Then `numbers[0]` contains how many numbers do you have and `numbers[1:]` are those numbers. In other way you probably need to use not-standard library. – ventaquil Jan 04 '20 at 08:42
  • @ventaquil in your comment numbers is a list, which I want to avoid using. – alekscooper Jan 04 '20 at 09:02
  • so you must use non-standard library to probably load data char by char from terminal. – ventaquil Jan 04 '20 at 13:14
  • @ventaquil I guess so. What libraries could you recommend, please? – alekscooper Jan 04 '20 at 17:31

1 Answers1

0

In the code you have provided, you are not storing an array: in effect you are just storing the last entered number, even though you are in a loop.

Below is the C++ code and the matching Python code. Both don't store individual entered values .. they store only the last value. If you want to store every value, I think some sort of data structure will be required.

The provided C++ program

int how_many_numbers, number; 
cin >> how_many_numbers; 
for (int i = 0; i < how_many_numbers; i++) 
{ cin >> number; 
// do something with it 
}

Python equivalent

how_many_numbers, number = 0, 0
how_many_numbers = input()
for (i in range(how_many_numbers)):
    number=input()
    # do something with it

Hope this helps.

Amit
  • 2,018
  • 1
  • 8
  • 12
  • input() in Python doesn't work the same way as cin in C++ regarding blank spaces and hitting the enter button. – alekscooper Jan 04 '20 at 17:31
  • 1
    @alekscooper Yes you are right. In C++ if you write multiple inputs (while you have declared a single int), all other entries after first will be ignored, in my opinion. In python, everything gets stored no matter how many numbers you type since the entire line is stored as a string. I thought you wanted closest Python counterpart of the C++ code. Sorry to have misread the question. – Amit Jan 05 '20 at 09:51