Reputation: 99
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?
Upvotes: 3
Views: 1154
Reputation: 15872
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.
Upvotes: 1
Reputation: 198
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
Upvotes: 0
Reputation: 114290
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']
Upvotes: 1
Reputation: 2343
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
Upvotes: 0