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?
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?
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().
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?
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) + '?'