Possible to get user input without inserting a new line?
But how do I stop raw_input from writing a newline?
In short: You can't.
raw_input()
will always echo the text entered by the user, including the trailing newline. That means whatever the user is typing will be printed to standard output.
If you want to prevent this, you will have to use a terminal control library such as the curses
module. This is not portable, though -- for example, curses
in not available on Windows systems.
How to input without newline in Python?
The newlines from the inputs are just local echos from the console. You see them on your screen but they aren't actually returned by the input
function.
The real problem is with the newline you explicitly print before every line number. Remove the \n
so the line that prints line numbers becomes:
print(" {}| ".format(i+1), end='')
Furthermore, the file you're loading may not necessarily have a trailing newline in the end, so you need to detect that and print a newline if that's the case. Note what I added after your first for
loop:
def PyCons(f):
file = open(f, "r")
appe = open(f, "a")
print("\n=====\nPyCons Editor, v1.2\nPython 3.6.1\n")
global i
i = 0
for line in file:
i += 1
print(" {}| ".format(i) + line, end='')
if not line.endswith('\n'):
print()
for k in range(10000000000000):
print(" {}| ".format(i+1), end='')
inp = input("")
if inp == "@PyCons.save()":
print("\n=====")
break
else:
i += 1
appe.write("\n" + inp)
How to avoid line break after user input in Python?
you mean something like that?
choice = input ("enter somthing...")
print ("Is it "+str(choice)+"?")
submit input in text area using return without inserting a newline character
You should prevent default behavior, which is inserting a new line, before calling your function.
if (event.keyCode == 13 && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
...
}
Java User Input without printing a newline
You'll have to repaint the whole screen, or use a curses library.
Sorry. I know that's not what you wanted to hear, but them's the breaks... It's a limitation of Consoles generally, which were designed to allow "dumb terminals" to communicate with a "smart server", and I/O was typically done on a "line by line" basis.
So you see why us modern programmers just love GUI's, HTML, and basically all things non-console... because console applications where complete, utter, total pain-in-the-arse to write, and they looked like shazbut too ;-)
Cheers. Keith.
Related Topics
How to Cleanly Uninstall Ansible
How to Adjust Padding With Cutoff or Overlapping Labels
Python - How to Sort a List of Alpha and Numeric Values
How to Avoid Last Comma in Python Loop
Python List - Only Keep Only-Positive or Only-Negative Values
Python Login Script; Usernames and Passwords in a Separate File
Python: How to Split a List Based on a Specific Element
Convert String from Big-Endian to Little-Endian or Vice Versa in Python
Set Working Directory in Python/Spyder So That It's Reproducible
How to Change the Title Bar in Tkinter
How to Call a Classes Method from Another Class Without Initialising the First Class
Swapping Columns in a Numpy Array
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
How to Find the Average of Particular Numbers in a CSV File
How to Check the Date Is Empty Using Python
Find Matching Rows in 2 Dimensional Numpy Array
I Want to Multiply Two Columns in a Pandas Dataframe and Add the Result into a New Column