Reputation: 52665
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?
Upvotes: 1
Views: 12202
Reputation: 52665
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?
Upvotes: 2
Reputation: 380
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) + '?'
Upvotes: 1
Reputation: 17
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) + " ?"
Upvotes: -2
Reputation: 369
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
Upvotes: 0
Reputation: 11531
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()
.
Upvotes: 2
Reputation: 7294
you mean something like that?
choice = input ("enter somthing...")
print ("Is it "+str(choice)+"?")
Upvotes: 1