Search code examples
pythonconsole

How to Remove the last few characters from console Python?


I am making a little math game, similar to zeta mac. Everything seems to be working well. Ideally I would like this console output to erase incorrect answers entered by the user, without reprinting the math problem again for them to solve. Is something like this possible?

For example, I may prompt the user to answer "57 + 37 = " in the console, then if they type 24 (console would look like this "57 + 37 = 24", I would like the 24 to be erased, and for the "57 + 37 = " to remain, allowing the user to guess again, without the same equation having to be printed again on a line below.

Here is the source code (sorry if its messy, I just started learning python):

import random
import time


def play(seconds):
    start_time = time.time()
    score = 0
    while True:
        current_time = time.time()
        elapsed_time = current_time - start_time
        a = random.randint(2, 100)
        b = random.randint(2, 100)
        d = random.randint(2, 12)
        asmd = random.choice([1, 2, 3, 4])
        if (asmd == 1):
            solve = a + b 
            answer  = input("%d + %d = " % (a, b))
        elif (asmd == 2):
            if (a > b):
                solve = a - b
                answer  = input("%d - %d = " % (a, b))
            else:
                solve = b - a
                answer  = input("%d - %d = " % (b, a))
        elif (asmd == 3):
            solve = a * d
            answer  = input("%d * %d = " % (a, d))
        else:
            solve = d
            c = a * d
            answer  = input("%d / %d = " % (c, a))
    
             
        while (solve != int(answer)):
            answer = input("= ")   
        score += 1
        if elapsed_time > seconds:
            print("Time\'s up! Your score was %d." % (score))
            break  


play(10)


Solution

  • Just use add these two lines after answer = input("= "):

    sys.stdout.write("\033[F") #back to previous line

    sys.stdout.write("\033[K") #clear line

    import random
    import time
    import sys
    
    def play(seconds):
        start_time = time.time()
        score = 0
        while True:
            current_time = time.time()
            elapsed_time = current_time - start_time
            a = random.randint(2, 100)
            b = random.randint(2, 100)
            d = random.randint(2, 12)
            asmd = random.choice([1, 2, 3, 4])
            if (asmd == 1):
                solve = a + b 
                answer  = input("%d + %d = " % (a, b))                            
            elif (asmd == 2):
                if (a > b):
                    solve = a - b
                    answer  = input("%d - %d = " % (a, b))
                else:
                    solve = b - a
                    answer  = input("%d - %d = " % (b, a))
            elif (asmd == 3):
                solve = a * d
                answer  = input("%d * %d = " % (a, d))
            else:
                solve = d
                c = a * d
                answer  = input("%d / %d = " % (c, a))
        
                 
            while (solve != int(answer)):
                answer = input("= ") 
                if solve != int(answer):  
                    sys.stdout.write("\033[F") #back to previous line 
                    sys.stdout.write("\033[K") #clear line
                
            score += 1
            if elapsed_time > seconds:
                print("Time\'s up! Your score was %d." % (score))
                break  
    
    
    play(10)