Python.org supposedly loves spam anyway, so long as you include some eggs.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
Sigh.
No, python doesn't have clear screen.
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
I suggest
>>> print("\n" * 40)
Or even better
>>> def clear():
... print("\n"*40)
>>> clear()
Last one:
>>> def clear():
... import sys
... for i in range(40):
... print(sys.ps1)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Clearing the display screen in console mode depends on the Operating System, that and the limited usefulness is why many multiplatform computer languages do not include it.
Here is a Python version using module os ...
import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
import os
# works on windows nt (also xp and vista) or linux
os.system(['<strong class="highlight">clear</strong>','cls'][os.name == 'nt'])
Can anyone kindly explain the background of how this bit of code works?
I suppose you pasted this from a web page. It doesn't work. The following works
import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])
if the system is nt, then os.name=='nt' returns True, which is converted to the integer 1 and ['clear','cls'][1] is 'cls', which is passed to os.system. On another system, 'clear' is passed.
This is old style coding. Nowadays, one writes
os.system('cls' if os.name == 'nt' else 'clear')
(By the way, using os.system is outdated too)
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
import os
# works on windows nt (also xp and vista) or linux
os.system(['clear','cls'][os.name == 'nt'])
If [os.name == 'nt'] is True then this becomes effectively 1. So it picks item at index 1 from the list which is the Windows command 'cls'. If it's False, it becomes 0 and the item at index 0 (zero) is used, which is the Linux command 'clear'.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184