-2

I have a problem with basic simple python. So I am making a memory game and I want to display the objects and after a delay of 5 seconds, make the text disappear.

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

this is a part of the code. Pls help me out here I want to hide the text obj = print("Ship, Money, Python") after a delay of 5 seconds.

TrexS.
  • 3
  • 5

4 Answers4

0

Ending with "\r" causes the next output to start at the beginning of the previous line. Then you can replace the output with an appropriate number of spaces (" " * 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")
mcsoini
  • 6,280
  • 2
  • 15
  • 38
0

EDIT: have simplified your check to print correctness of the input.

Try out this:

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")
Bhagyesh Dudhediya
  • 1,800
  • 1
  • 13
  • 16
0

Here is a working solution. It uses \r to come back at the beginning of the line and \033[K to clear the line:

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")
mozway
  • 194,879
  • 13
  • 39
  • 75
0

How about to use buffer of showing sentences.

This is one of idea. you can modify some code lines.

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


Edit: if you got an warning sentence that is "TERM environment variable not set.", then you should check the value TERM of your terminal.

ex) $ export TERM=xterm

SEUNGFWANI
  • 140
  • 10
  • let me check, doesn't work, my friend – TrexS. Sep 08 '21 at 07:34
  • Does it mean that "doesn't work" is an if statement? or printing function? – SEUNGFWANI Sep 08 '21 at 07:40
  • You should change your if statement on your code. Because this question's point is "how to hide already printed sentence" – SEUNGFWANI Sep 08 '21 at 07:43
  • I do not understand what u are trying to say...See, doesn't work means that the code u gave me doesn't function properly – TrexS. Sep 08 '21 at 07:47
  • Uhmm... how about set environment of your terminal before running. like `export TERM=xterm`. And if you use PyCharm, then you can try to check an option (`Emulate terminal in output console`) in `Debug configurations`. – SEUNGFWANI Sep 08 '21 at 07:55