【问题标题】:How do I clear printed text? [duplicate]如何清除打印的文本? [复制]
【发布时间】:2019-12-12 04:38:37
【问题描述】:

我在清除 python 3.8 中的文本时遇到问题。我看过类似的答案,但他们没有帮助我解决我遇到的问题,因为这些答案提供的解决方案对我不起作用。这是代码行:

def userInstructions(userInput):
    if userInput == "help" or userInput == "Help":
        help()
    elif userInput == "list" or userInput == "List":
        words()
    elif userInput == "tutorial" or userInput == "Tutorial":
        tutorial()
    elif userInput == "salve":
        LatinRef.salve()

    return input('\nEnter your responce: ')
    os.system('clear')

我想要做的是每次用户输入响应时,响应将被清除并且它将是空白而不是像这样显示:

Type in your command or any Latin word to review or learn.

Enter your responce: hello
Type in your command or any Latin word to review or learn.

Enter your response: help 

我该如何解决这个问题?如果有帮助,我目前正在使用 python 3.8。

【问题讨论】:

  • 这是在什么操作系统上运行的?
  • 这是我第一次使用 os,所以我不太清楚,但我在 python 3.8 中使用它。 @gdanton
  • 如果是windows你必须使用'cls'
  • @gdanton 我正在使用 MacOs
  • 代码中的os.system('clear') 永远不会被执行,因为return input('\nEnter your responce: ') 就在它退出函数之前。

标签: python python-3.x macos printing console


【解决方案1】:

按照https://stackoverflow.com/a/518007/1244045 中的说明尝试此操作

clear = lambda: os.system('clear')
clear()

'cls' 用于 Windows。

【讨论】:

  • Type in your command or any Latin word to review or learn. Enter your responce: he Type in your command or any Latin word to review or learn. Enter your responce: help Type in your command or any Latin word to review or learn. list of commands help = Display of commands list = list of all the Latin I vocabulary tutorial = in depth tutorial Quit = exits the main program to the exit credits and then exits the app Enter your responce: Quit
  • 仍然是我想避免的结果,只是把自己放在另一行
  • 嗯..奇怪。 Python 是否安装在 MacOS 中并且您正在从终端运行程序?
  • 好的,应该可以了。如果你想从界面做,唯一的方法似乎是通过打印换行符来伪造它 - stackoverflow.com/questions/1432480/…
  • 不用担心。很高兴它成功了!
【解决方案2】:

基于操作系统的清屏脚本

from os import name, system

# windows
if name == 'nt':
    system('cls')
# linux
else:
    system('clear')

【讨论】:

  • 总是为你的答案添加解释。
猜你喜欢
  • 2019-12-04
  • 2017-12-29
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 2014-08-04
  • 1970-01-01
相关资源
最近更新 更多