1

So in Python, the user places input on a line in the console and it stays there. I want the code to basically print over the previous input line, covering the user input on the last line with a printed string. Is there any way to do that?

How many planets are in the solar system?

#user inputs answer on this line

--->

How many planets are in the solar system?

correct

Bebop 100
  • 11
  • 1

1 Answers1

1

A quick fix would be to clear the console with os.system('cls') and re-print the question.

Code:

import os
print('How many planets are in the solar system?')
value = int(input())

os.system('cls')
print('How many planets are in the solar system?')
if value == 8:
    print('Good job you are right!')
else:
    print('You are wrong.')

Output:

Before the clear:

How many planets are in the solar system?
3

After the clear:

How many planets are in the solar system?
You are wrong.

This works great and doesn't require much code.

Blue Robin
  • 847
  • 2
  • 11
  • 31
  • I should've stated this earlier, but there's already text printed before the input which I still want to stay there, so clearing the entire console doesn't work for my situation. Thank you though Blue Robin! – Bebop 100 Mar 17 '22 at 12:46
  • Oh @Bebop100 I’ll try and figure something out! – Blue Robin Mar 17 '22 at 23:08
  • @Bebop100 I couldn't really find anything. Maybe you can try to reprint all the past output and do this by storing everything in a list? – Blue Robin Mar 18 '22 at 00:17
  • That is a possibility, but it seems a little annoying. If that's the best I have though, I'll definitely do that. – Bebop 100 Mar 18 '22 at 13:37
  • It might be @Bebop100 but I'm not sure. – Blue Robin Mar 18 '22 at 22:04
  • @Bebop100 I found this post though: https://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output the top answer didn't work for me but it could work – Blue Robin Mar 18 '22 at 22:06