1

How to change below code and remove unnecessary new line?

#"Please guess a number!"
choice = input ("Is it ")
print ("?")

Is it 10

?

So that result will look like

Is it 10?

Andersson
  • 51,635
  • 17
  • 77
  • 129

6 Answers6

2

To "remove" the newline in your code snippet you would need to send cursor control commands to move the cursor back to the previous line. Any concrete solution would depend on the terminal you're using. Strictly speaking, there are no unnecessary newlines in the sample above. The user provided the newline that follows 10, not Python. I suppose you could try to rewrite the input processor so that user input isn't echoed similar to getpass.getpass().

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
2

If someone looking for a solution, I found one-liner:

print('Is it ' + input('Please guess a number: ') + '?')
  • Firstly, with above line user input requested as

    Please guess a number:
    
  • Then user enter the value

    Please guess a number: 10
    
  • After input confirmation (Pressing Enter) next line appears as

    Is it 10?
    
Andersson
  • 51,635
  • 17
  • 77
  • 129
1

you mean something like that?

choice = input ("enter somthing...")
print ("Is it "+str(choice)+"?")
Shoham
  • 7,014
  • 8
  • 40
  • 40
  • 1
    this adds an extra line to stdout which i think is not what the question intended. A very similar solution is [here](http://stackoverflow.com/questions/12586601/remove-last-stdout-line-in-python) that allows you to remove the line created by the input function – Gil May 20 '15 at 15:56
0

If you need it to be longer then the above one you could drag it out.

Choice = input("Number:") answer = "Is it %s ?" % choice print(answer)

I like Shohams tho, more pythonic imho

Scott Johnson
  • 369
  • 2
  • 15
0

Very similar to the accepted answer in this question: remove last STDOUT line in Python

But here it is tailored to your solution. A little hackish admittedly, would be interested in a better way!

choice = input ("Is it ")
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
print CURSOR_UP_ONE + ERASE_LINE + 'Is it ' + str(choice) + '?'
Community
  • 1
  • 1
Gil
  • 370
  • 2
  • 11
-2

I do deep you cannot.Because if the procedure is "choice = input("Is it ")",you must be to what end the enter,the line must be is new line.you can the change the express. choice = input("Please input the number:") print "Is it " + str(choice) + " ?"

Linfeng Yu
  • 17
  • 3