3

I made a loop that lets the user input numbers until he enters '13', then the loop will break.

while True:
       number = int(input())
       if number == 13:
           print("goodbye")
           break

When I run this program it looks like this:

1
2
3
13
goodbye

But I want the input to continue in the same line whenever I hit enter like this:

1 2 3 13
goodbye

How can I do that?

  • 1
    If you want to be able to press enter between the entry of each number, this isn't possible. – scotty3785 Jan 07 '21 at 09:38
  • 1
    I don't think it is possible to do so because every time you give an input, it will print in other line because input doesn't handle arguments like `end = ''` – QuantumOscillator Jan 07 '21 at 09:49
  • 1
    Yeah, ANSI control characters would work, but it depends on where OP is running the program. Windows IDLE would fail. – Sayandip Dutta Jan 07 '21 at 10:01
  • Does this answer your question? [Possible to get user input without inserting a new line?](https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line) – Georgy Jan 07 '21 at 11:59

4 Answers4

1

You need something that lets you read characters one at a time without waiting for the newline. This question offers a number of solutions. I've made a small example here that uses the getch wrapper, which should be pretty cross-platform (installable via pip or similar):

from sys import stdout
from getch import getch

lines = []
line = []

while True:
    k = getch()
    if k == '\n':
        item = ''.join(line)
        line.clear()
        lines.append(item)
        try:
            if int(item) == 13:
                stdout.write(k)
                break
        except ValueError:
            pass
        stdout.write(' ')
    else:
        stdout.write(k)
        line.append(k)
    stdout.flush()

print('goodbye!')
print(lines)

While this will not have features like a working backspace out of the box, it does what you want for correct input and you can probably implement them. Here is a sample run for input asdf\n48484\n13\n:

asdf 48484 13
goodbye!
['asdf', '48484', '13']
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
1

One relatively easier way would be:

import os
prompt = ''
while True:
    number = input(prompt)
    os.system('cls')
    # prompt += number + ' ' #If you want the control input to be printed, keep this, remove the other
    if int(number) == 13:
        print(prompt)
        print('goodbye')
        break
    prompt += number + ' '    # add this line before the if starts, if you want to keep 13
    os.system('cls')

Output:

1 2 3 4
goodbye

Notice, this does not print 13 as you wanted in the comments. However, if you want to keep it, you can move it to the line before the if condition starts.

This works in Windows command prompt, cannot guarantee about different IDLEs/iPython etc.

NOTE: This clears all lines in the console, and rewrites them. This works for your particular problem, but may not be ideal if something else has also been printed before. If you want to avoid that, you will have to redirect sys.stdout to a file or some kind of string buffer before you hit this part of code, then reinstate stdout and store the content of the buffer as text in the prompt variable used above.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
0

It is possible, but the solution is system dependent. This should work for windows 10, but there is a chance that you have to change some settings for it (and right now I do't know which EDIT: This Setting ). The trick is to use ansi control characters, that are used to change the console:

import sys

UP_ONE_LINE = "\033[A" # up 1 line
DELETE_LINE = "\033[K" # clear line 

number = []
while True:
        #number = int(input())
        number.append(int(input()))
        sys.stdout.write(UP_ONE_LINE)
        sys.stdout.write(DELETE_LINE)
        print(number,end="")
        if number[-1] == 13:
           print("\ngoodbye")
           break

It deletes the last line and then writes all the numbers. You could also use the control characters to go to the end of the last line, but those were the ones I used. In my console the result is:

[1, 2, 3, 4, 13]
goodbye
Finn
  • 2,333
  • 1
  • 10
  • 21
0

Python continue from input with the space key

import sys
import getch
input = ""
while True:
    key = ord(getch.getch())
    sys.stdout.write(chr(key))
    sys.stdout.flush()
    # If the key is space
    if  key != 32:
        input += chr(key)
    else:
        input =   ""
    if input == "13":
        print("\nbye")
        break

sepideh_ssh
  • 200
  • 2
  • 8