2
for i in range(1, 10):
    print('temp {}: '.format(i), end = '')
    temp = float(input(''))
    print(' Celsius = {} Fahrenheit'.format(9 / 5 * temp + 32))

output is:

temp 1: 0
Celsius = 32 Fahrenheit
temp 2: 5
Celsius = 41 Fahrenheit

I want print ' Celsius =...' in previous line.

I want to output be:

temp 1: 0 Celsius = 32 Fahrenheit
temp 2: 5 Celsius = 41 Fahrenheit
H.R Sh
  • 37
  • 8
  • 1
    Unrelated to your question, but the proper spelling is "Fahrenheit" – Jonathan Hall Aug 27 '18 at 13:18
  • The issue here is whenever you use either input() in py3 or raw_input() in py2. It reads the promp and prints the prompt. And moves to the new line. If print statements were used consecutively, using "," in py2 and end= ' ' in py3. Those print statements will be printed in the same line. But input() or raw_input() prints the prompt and later move to next line in STDOUT. – Raviteja Ainampudi Aug 27 '18 at 14:05
  • Found a similar question like yours. https://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line According to that when we use raw_input() or input() within/through print statements, we cannot print them in same line. Unless we use **Curses** module. – Raviteja Ainampudi Aug 27 '18 at 14:18

3 Answers3

1

I found!!! Using the @nik-k answer in post Possible to get user input... :

I added print('\033[{}C\033[1A'.format(6 + len(str(temp))), end = '') before the last line. '\033[{} (6 for going to previous line and len(...) to move the cursor forward) C\033[1A'

for i in range(1, 10):
    print('temp {}: '.format(i), end = '')
    temp = float(input(''))
    print('\033[{}C\033[1A'.format(6 + len(str(temp))), end = '')
    print(' Celsius = {} Fahrenheit'.format(9 / 5 * temp + 32))
H.R Sh
  • 37
  • 8
0

Using input() needs you to press enter, which will add the new line. I would go around that by first inputting the temperature and only then printing the whole x C = y F line.

Honza Zíka
  • 495
  • 3
  • 12
  • I know this, but is there any way to going cursor to the previous line and then printing? – H.R Sh Aug 27 '18 at 13:27
  • That is not possible. The only way I can think of is clearing the console and writing everything again, which could give the perception of "no inserted newline", but of course that doesn't take other outputs into account (for example dir name and script name when it was run) and is usually quite unreliable. – Honza Zíka Aug 27 '18 at 13:30
  • It is possible to switch the terminal to no-echo mode (like password input) and raw mode (char by char input, not line by line), but then your program would be responsible for all line editing. – VPfB Aug 27 '18 at 14:22
0

I believe in python 2.x you could use :

print('temp {}: '.format(i), end = ''),

That , after first print will do the job but if you are using python 3. I suggest you to see this :

Print new output on same line

Lorelorelore
  • 3,335
  • 8
  • 29
  • 40
Benjamin
  • 13
  • 3
  • No need to put a comma after after print() and this line with "..., end=' ' " works properly. The problem is in the second line where input() needs you to press enter, which will add the new line and I need a way to go cursor to the previous line and then printing. – H.R Sh Aug 27 '18 at 13:33