【问题标题】:Escape Sequences in Python Not Working in CMDPython 中的转义序列在 CMD 中不起作用
【发布时间】:2016-11-12 17:25:48
【问题描述】:
import time
listy = ['timer','stopwatch']

def intro():
    print("This is a program which contains useful tools")
    print(listy)

def timer():
    x = int(input("How long Seconds ?"))
    while x > 0:
        print(x)
        time.sleep(1)
        x -= 1

def stopwatch():
    verif = input("Do you want to start y/n \n")
    if verif == 'y':
        x = 0
        while True:
            print(x, end = "\b"*5)
            time.sleep(1)
            x += 1

def main():
    intro()
    decider = input("Which Program?")
    if decider.lower() == 'timer':
        timer()
    elif decider.lower() == 'stopwatch':
        stopwatch()

main()

在这段代码中,我不知道为什么 \b 转义序列不能在 cmd 或空闲状态下工作,谁能解释为什么?是不是因为逻辑错误?

【问题讨论】:

  • 如果您将其替换为另一个(可打印的!)字符,您是否看到它出现在您期望的位置?如果你不这样做,那就是逻辑错误。
  • 是的,但我希望它只计入同一行
  • "是的" = "是的,我看到它出现了"?这有关系吗? stackoverflow.com/q/9667462/2564301

标签: python cmd python-idle


【解决方案1】:

可能需要冲洗。怎么样...

print("{0}{1}".format("\b"*5, x), end="", flush=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2013-12-20
    • 2023-03-15
    • 1970-01-01
    • 2013-01-29
    • 2022-01-25
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多