【问题标题】:Python - Hide already printed text [duplicate]Python - 隐藏已打印的文本[重复]
【发布时间】:2021-11-04 22:58:42
【问题描述】:

我对基本的简单 python 有疑问。所以我正在做一个记忆游戏,我想显示对象,延迟 5 秒后,让文本消失。

import time

print("Welcome to the memory game.")
print("Choose difficulty level :")
print("1. Easy")
print("2. Medium")
print("3. Hard")
print("4. Very Hard")
x = input("press '1' for easy, '2' for medium, '3' for hard and '4' for very hard :  ")

if x == '1' :
    print("You chose level 1, easy.")
    print("I will give you 3 objects and you have to type then in the correct order to gain points.")
    print("Not cAsE SeNSitIvE")
    obj = print("Ship, Money, Python")
    time.sleep(5)
    ez = input().upper
    if ez == 'Ship, Money, Python' or ez == 'ship,money,python' or ez == 'Ship,Money,Python' or ez == 'ship, money, python' :
        print("Correct")
    else :
        print("Wrong")
        print("The answer was : Ship, Money, Python")

这是代码的一部分。请帮我在这里????我想隐藏文字 obj = print("Ship, Money, Python") 延迟 5 秒后。

【问题讨论】:

  • 谁能帮帮我?
  • 如果你想从终端清除文本,你可以休眠 5 分钟,然后使用 clear 命令。 os.system('cls' if os.name == 'nt' else 'clear') 你必须导入 os 并休眠
  • 这能回答你的问题吗? :stackoverflow.com/questions/5290994/…
  • 让我看看@AlexeyLarionov
  • 呃,不,你能不能再给我一个解决方案

标签: python


【解决方案1】:

以"\r" 结尾会导致下一个输出从上一行的开头开始。然后,您可以将输出替换为适当数量的空格 (" " * len(text))。

# import time

print("Welcome to the memory game.")
print("Choose difficulty level :")
print("1. Easy")
print("2. Medium")
print("3. Hard")
print("4. Very Hard")
x = input("press '1' for easy, '2' for medium, '3' for hard and '4' for very hard :  ")

if x == '1' :
    print("You chose level 1, easy.")
    print("I will give you 3 objects and you have to type then in the correct order to gain points.")
    print("Not cAsE SeNSitIvE")
    text = "Ship, Money, Python"
    obj = print(text, end="\r")
    time.sleep(1)
    print(" " * len(text), end="\r")
    ez = input().upper()
    if ez in [string.upper() for string in 
              ['Ship, Money, Python',
               'ship,money,python',
               'Ship,Money,Python', 
               'ship, money, python']]:
        print("Correct")
    else :
        print("Wrong")
        print("The answer was : Ship, Money, Python")

【讨论】:

  • 嘿,让我检查一下并回复你,给我一分钟
  • 不,没有解决问题
  • 代码完美隐藏,但即使输入正确答案,它也会显示“错误”“正确答案是......”
  • 这是一个不同的问题。请检查 input().upper() 的样子(你缺少括号,因为 upper 是一个函数)并考虑你的比较是否有意义
  • 小更新:也使用end="\r" 作为空格以避免换行
【解决方案2】:

编辑:已简化您的检查以打印输入的正确性。

试试这个:

import time
import re

print("Welcome to the memory game.")
print("Choose difficulty level :")
print("1. Easy")
print("2. Medium")
print("3. Hard")
print("4. Very Hard")
x = input("press '1' for easy, '2' for medium, '3' for hard and '4' for very hard :  ")

if x == '1' :
    print("You chose level 1, easy.")
    print("I will give you 3 objects and you have to type then in the correct order to gain points.")
    print("Not cAsE SeNSitIvE")
    text = "Ship, Money, Python"
    obj = print(text, end="\r")
    time.sleep(5)
    print(" " * len(text))

    ez = str(input()).upper()
    if re.match(r"SHIP\s*,\s*MONEY\s*,\s*PYTHON", ez):
        print("Correct")
    else :
        print("Wrong")
        print("The answer was : Ship, Money, Python")

【讨论】:

  • 好的让我试试
  • 不,有2个问题1)清除所有代码,所有文本
  • 2) 它仍然说'错误''正确答案是..'
  • 输入的内容是什么?
  • 让我尝试编辑
【解决方案3】:

这是一个可行的解决方案。它使用\r 回到行首,\033[K 清除行:

import time

print("Welcome to the memory game.")
print("Choose difficulty level :")
print("1. Easy")
print("2. Medium")
print("3. Hard")
print("4. Very Hard")
x = input("press '1' for easy, '2' for medium, '3' for hard and '4' for very hard :  ")

if x == '1' :
    print("You chose level 1, easy.")
    print("I will give you 3 objects and you have to type then in the correct order to gain points.")
    print("Not cAsE SeNSitIvE")
    obj = print("Ship, Money, Python", end='\r', flush=True)
    time.sleep(5)
    print('\033[K', end='', flush=True)
    ez = input().upper
    if ez == 'Ship, Money, Python' or ez == 'ship,money,python' or ez == 'Ship,Money,Python' or ez == 'ship, money, python' :
        print("Correct")
    else :
        print("Wrong")
        print("The answer was : Ship, Money, Python")

【讨论】:

  • 即使我输入正确,它也说错了
【解决方案4】:

如何使用显示句子的缓冲区。

这是一种想法。你可以修改一些代码行。

import os
import time

buffer = [
    "Welcome to the memory game.",
    "Choose difficulty level :",
    "1. Easy",
    "2. Medium",
    "3. Hard",
    "4. Very Hard",
    "press '1' for easy, '2' for medium, '3' for hard and '4' for very hard :  ",
]


def printing():
    os.system('clear')
    for line in buffer:
        print(line)


printing()
x = input()

if x == '1':
    buffer.append("You chose level 1, easy.")
    buffer.append("I will give you 3 objects and you have to type then in the correct order to gain points.")
    buffer.append("Not cAsE SeNSitIvE")
    printing()
    print("Ship, Money, Python")
    time.sleep(5)
    printing()
    ez = input()
    if ez == 'Ship, Money, Python' or ez == 'ship,money,python' or ez == 'Ship,Money,Python' or ez == 'ship, money, python':
        print("Correct")
    else:
        print("Wrong")
        print("The answer was : Ship, Money, Python")


编辑: 如果您收到“未设置 TERM 环境变量”的警告语句,则应检查终端的值 TERM。

例如)$ export TERM=xterm

【讨论】:

  • 让我检查一下,不起作用,我的朋友
  • 这是否意味着“不起作用”是一个if语句?还是打印功能?
  • 您应该更改代码中的 if 语句。因为这个问题的重点是“如何隐藏已经打印的句子”
  • 我不明白你在说什么......看,不起作用意味着你给我的代码不能正常工作
  • 嗯...在运行之前设置终端环境怎么样。喜欢export TERM=xterm。如果你使用 PyCharm,那么你可以尝试检查 Debug configurations 中的一个选项 (Emulate terminal in output console)。
猜你喜欢
  • 2016-06-19
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2011-09-14
  • 1970-01-01
相关资源
最近更新 更多