如何从控制台 Python 中删除最后几个字符?

How to Remove the last few characters from console Python?

我正在制作一个类似于 zeta mac 的小数学游戏。一切似乎都运作良好。理想情况下,我希望此控制台输出能够清除用户输入的错误答案,而无需再次重新打印数学问题供他们解决。这样的事情可能吗?

例如,我可能会提示用户在控制台中回答“57 + 37 =”,然后如果他们键入 24(控制台看起来像这样“57 + 37 = 24”,我希望 24被擦除,并保留“57 + 37 =”,允许用户再次猜测,而不必在下面的一行上再次打印相同的等式。

这里是源码(内容比较乱请见谅,刚开始学习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)

只需在 answer = input("= "):

之后添加这两行

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

sys.stdout.write("3[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("3[F") #back to previous line 
                sys.stdout.write("3[K") #clear line
            
        score += 1
        if elapsed_time > seconds:
            print("Time\'s up! Your score was %d." % (score))
            break  


play(10)

从输入的末尾删除字符可能不是最好的方法,因为您不太确定会有多少个字符(取决于数字)。

@QualidSai 的回答确实清除了终端,但它不会再次显示您的公式字符串。为了解决这个问题,我会将您作为输入的一部分打印的公式字符串存储为字符串变量,然后您可以在循环时使用它。例如:

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 
            question = "%d + %d = " % (a, b)
        elif (asmd == 2):
            if (a > b):
                solve = a - b
                question = "%d - %d = " % (a, b)
            else:
                solve = b - a
                question = "%d - %d = " % (b, a)
        elif (asmd == 3):
            solve = a * d
            question = "%d * %d = " % (a, d)
        else:
            solve = d
            c = a * d
            question = "%d / %d = " % (c, a)
        
        answer = False
        while (solve != int(answer)):
            answer = input(question)
            sys.stdout.write("3[F")
            sys.stdout.write("3[K")
        score += 1
        if elapsed_time > seconds:
            print("Time\'s up! Your score was %d." % (score))
            break  

play(10)