Bebop 100
Bebop 100

Reputation: 11

How do I print over a previous line which took in input? Python

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

Upvotes: 1

Views: 521

Answers (1)

Blue Robin
Blue Robin

Reputation: 1101

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.

Upvotes: 1

Related Questions