Python隐藏已打印的文本

2024-03-29 08:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我对基本的简单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秒后


Tags: ortheto文本forinputtimeeasy
3条回答

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

试试这个:

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")

"\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")

这里有一个有效的解决方案。它使用\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")

相关问题 更多 >