-1

I searched the first page of Google but couldn't find an answer. This won't happen for int(raw_input()) weirdly enough. But if i type:

a = [raw_input() for i in range(int(raw_input()))]
print(a)

Example input: 5 1 2 3 4 5

The output will be: ['1\n', '2\n', '3\n', '4\n', '5\n']

I know this has something to do with the IDE (I run VSCode) but I have no clue how to fix it. Thank you.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
Qiu Tony
  • 3
  • 2
  • just use `int(input())` instead of `int(raw_input())`. Raw input will return you all the details including the \n – Joe Ferndz Jan 27 '21 at 00:50
  • https://www.geeksforgeeks.org/difference-between-input-and-raw_input-functions-in-python/. Also see more information here https://stackoverflow.com/questions/5563089/raw-input-function-in-python – Joe Ferndz Jan 27 '21 at 00:51
  • @JoeFerndz Thanks, but that's not my question, int(raw_input()) works fine. But raw_input() at some random point today began adding \n to each input when it never did that before. I need to use raw_input because it shaves off a decent amount of time off my CP submissions. I'm not interested in the niche uses of raw_input, I'm looking at how to fix this bug. – Qiu Tony Jan 27 '21 at 00:56
  • raw_input() will always return a string value. You need to convert it to an integer if you want integer. – Joe Ferndz Jan 27 '21 at 00:58
  • btw, if you are using 3.9 or above, PEP 3111: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()). – Joe Ferndz Jan 27 '21 at 00:59
  • @JoeFerndz thanks for replying but that's not my question. Raw_input never did this before, and it's clearly a mistake of my IDE of some sort. I'm asking how to reverse this. – Qiu Tony Jan 27 '21 at 01:00
  • @JoeFerndz I think that could be the answer thanks – Qiu Tony Jan 27 '21 at 01:02
  • As far as i know, raw_input (on any IDE) should return you a string. You have to explicitly convert it into an int() if you want it to be an int – Joe Ferndz Jan 27 '21 at 01:03
  • btw. what version of python are you using? – Joe Ferndz Jan 27 '21 at 01:04
  • @JoeFerndz yes I'm aware raw_input will convert integer inputs into strings automatically. But it has never added \n at the end for no reason it seems. I experimented and type input = sys.stdin.readline and it's actually giving me the same problem – Qiu Tony Jan 27 '21 at 01:05
  • See if this helps. Not sure if this is something you can try. https://github.com/microsoft/vscode-python/issues/10529 – Joe Ferndz Jan 27 '21 at 01:06
  • i will vote to keep the question open so others in the community can provide guidance to your problem. – Joe Ferndz Jan 27 '21 at 01:08

1 Answers1

0

In Python3.x, raw_input() and input() are integrated, raw_input() is removed, only the input() function is retained, which receives any input, defaults all input to string processing, and returns the string type.

When use Python2.7:

enter image description here

When use Python3.8:

enter image description here

In addition, if you use extensions for debugging other than the python extension in VS Code, please try to disable it to avoid affecting the results.

My environment:

VS Code: 1.52.1 (user setup); OS: Windows_NT x64; VS Code extension: Python; Pylance;

Reference: What's new in Python3.

Jill Cheng
  • 9,179
  • 1
  • 20
  • 25