Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
WAN CHU
8,087 PointsWhat's the appropriate way to clear the screen in python?
Greeting, for the battleship project one requirement said: "The screen is cleared after an invalid entry." But how can we clear the screen? I have two solution but they both have some problem
- print('\n' * 20) This approach will make the alignment from the bottom of the screen
- import os; os.system('clear') This approach is os specific and didn't work in IDE like pyCharm ( Error: TERM environment variable not set. )
Is there any better way to clear the screen?
2 Answers
Kourosh Raeen
23,733 PointsYou can use os.name to use the right command based on the operating system:
import os
os.system('cls' if os.name == 'nt' else 'clear')
WAN CHU
8,087 PointsBut pyCharm still giving me the error TERM environment variable not set.
Simon Dein
4,772 PointsI have the same problem and still haven't figured it out.
Did you find a solution?
James Hoffman
6,354 PointsJames Hoffman
6,354 PointsYou are right that import os; os.system('clear') does not work in the PyCharm IDE but it works in terminal.
However, the error can be fixed by entering the following line in your .bash_profile: export TERM=xterm-color (fyi .bash_profile is a hidden file in your home (i.e. ~) directory.)