I know there are already many questions on this, so my question will be considered as duplicate but I am asking because I tried all most all of them and none of them worked.
I have a Hangman code, written in R which I am trying to convert into python. In R whenever a user enters a letter guess, my code outputs a Hangman's ASCI art and it does after clearing the console every time with this code cat('\f')
. Which I am trying to replicate in Python but I don't found any working code.
I searched for this on google and tried many things,
import os
def clear():
os.system('cls')
clear()
# or
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
Answer from Clear/overwrite standard output in Python
print('\033[H\033[J')
Source lost!
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.hotkey('command', 'l')
# I changed the clear all hotkey to CNTRL+L (but i doesn't work even when I use it manually after
# clicking on console)
This code snippet is from this question Clear PyCharm Run Window
All of them worked on the command prompt but none of them in pycharm. They show a weird box with a question mark
you can see how it looks like when I perform it
import os
def clear():
os.system('cls')
print('\n Hey there'*2)
clear()
print('\n Bye'*2)
output
However, I tried this code snippet, which worked but the problem here is that my program takes input and while I am giving input, it detects my mouse position( which will be obviously on the terminal, not the trach icon, while typing) and clicks on the terminal. so overall it doesn't worked as well
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # this line was not added in original source
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it