代码之家  ›  专栏  ›  技术社区  ›  SDC123

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

  •  2
  • SDC123  · 技术社区  · 2 年前

    我正在做一个小数学游戏,类似于 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)
    
    
    2 回复  |  直到 2 年前
        1
  •  0
  •   OualidSai    2 年前

    只需在后面添加这两行 答案=输入(“”) :

    系统。标准输出。写(“\033[F”)#回上一行

    系统。标准输出。写入(“\033[K”)\35;清除行

    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)
    
        2
  •  0
  •   Matty    2 年前

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

    来自@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("\033[F")
                sys.stdout.write("\033[K")
            score += 1
            if elapsed_time > seconds:
                print("Time\'s up! Your score was %d." % (score))
                break  
    
    play(10)