Python's input function reads up to the next line following the new line code on PyCharm.

Asked 1 years ago, Updated 1 years ago, 83 views

We recently installed PyCharm Community Edition with Anaconda Plugin 2019.1.3.x64 on Windows 7 (SP1).So if you're running Python 3.7 code and you have multiple input functions and multiple lines of standard input:

#coding:utf-8

line1 = input("Please enter line 1:")
print("Line 1:"+line1)
line2 = input("Please enter line 2:")
print("Line 2:"+line2)

If you enter the following two lines at once from standard input,

1111
2222

If you do "Normal execution in PyCharm", the variable line1 reads "line1 = '1111\n2222' all at once (line feed code does not stop).Please let me know if there is anything I can do to change the settings and make the intput function function work properly. (The output below is from the Python console.)

Please enter line 1:>?1111
... 2222
Line 1: 1111
2222
Please enter line 2:>? 
Line 2: 

Note that in the same PyCharm environment, in debug mode (green bug buttons), the input function works fine, reading only one line up to the new line code 」\n までの (below is the console screen output of a successful debugger).

 Connected to pydev debugger (build 192.5728.105)
Please enter line 1: Line 1: 1111
Please enter line 2: line 2: 2222
import sys;print('Python%son%s'%(sys.version,sys.platform))
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64bit (AMD64)] on win32

I was told by official support that I cannot ask questions unless I have a license agreement, so I asked you a question here.Thank you for your cooperation.

python python3 anaconda pycharm

2022-09-30 21:43

2 Answers

It's not a response, it's a different environment.
In the following environments, debug mode and normal execution worked the same way.

PyCharm 2019.2 (Community Edition)
Runtime version: 11.0.3 + 12-b304.10 amd64
VMs:OpenJDK64-Bit Server VM by JetBrains.r.o

Windows 10 Home 64-bit 1803 Virtualenv
Python 3.6.0

<\n should be replaced with a new line character

1111\n2222\nWhat happens when you copy

Please enter line 1:1111
2222
Line 1: 1111
Please enter line 2: line 2: 2222

Process finished with exit code 0

1111\n2222 behavior when copied (no last line feed)

Please enter line 1:1111
22221st line: 1111
Please enter line 2: 

Display after pressing Enter

Please enter line 1:1111
22221st line: 1111
Please enter line 2: 
Second line: 2222

Process finished with exit code 0

Changing the OS, virtual environment, and PyCharm version might resolve the problem.


2022-09-30 21:43

As with Payneco's verification, I tried with the second line feed code, but it didn't work.After pasting, press Enter twice.The second line feed code is not found on the screen.

Variable Contents

 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64bit (AMD64)]
Please enter line one: > ?1111
2222
Line 1: 1111
2222
Please enter line 2:>? 
Line 2: 

So I added the third line to the input data.

1111
2222
3333

If you run it, it will look like the following:The contents of the variable are

Variable contents (when input data is 3 lines)

Please enter line 1:>?1111
... 2222
... 3333
... 
Line 1: 1111
2222
3333
Please enter line 2:>? 
Line 2: 

For the time being, I will try to avoid typing all at once or running in debug mode.
Thank you.


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.