Possible to Get User Input Without Inserting a New Line

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



Leave a reply



Submit