This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 7 years ago.
I want to do something like this.
calculating 50%
calculating 60%
calculating 70%
but in a single line.
Did hours of googling and couldn't find anything. :)
You can do something like this:
print '50%%',
print '\r60%%',
print '\r70%%',
The comma makes sure there is no newline. The \r clears the current line, so that the previous 50% is removed, and overwritten with the 60%. However, because the print is not flushed (like without the comma), it can happen that you will not see some lines printed. For that, you'll need to flush the output, by using this:
import sys
sys.stdout.flush()
It depends whether you want to overwrite the previous thing or to really write all three statements on one line.
The latter you can do by
import sys
sys.stdout.write("calculating 50%")
sys.stdout.write("calculating 60%")
sys.stdout.write("calculating 70%")
The former would be probably achieved by terminal escape sequences an would be different for different OSs.
Related
This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 1 year ago.
I want to make a game in the python console, so I need to write out lines, and then re-write them. I started building up code for it, and came up with this:
import sys
while 1:
#I will calculate what to write here, and store it in display
display = ["thing", "other thing", "2nd other thing"]
#Move the writing start back to the beginning
for x in display: sys.stdout.write("\r")
#Write the contents of display
for x in display: sys.stdout.write(x + "\n")
However, the code does not erase the previously written text. It just repetitively prints the display list. How can I make it erase the text?
Edit:
Similar Answer How to overwrite the previous print to stdout in python?
Method:
You can do this by printing out as many whitespaces as you have characters on that line
However this would only be possible in a clean manner if you know the length of everything being printed.
For example if you know the length you can do the following
print("\r")
print(" " * length_of_line)
print("\r")
Otherwise if printing long line isn't a concern, you can adopt the brute-force method of print a load of whitespaces and hope it overwrites the whole line
print("\r")
print(" " * a_large_number)
print("\r")
You cannot use the "\r" separately from the print statement and I think you can only use it in this way with the print statement.
An implementation with the print statement would look like this:
for i in range(3): #No endless loop
display = ["thing", "other thing", "2nd other thing"]
for x in display: print(x, end="\r")
This would result in every line overwriting the previous one.
Also have a look here, there they also discuss different methods.
I want to write a code which prints Loading, and then erases that and prints Loading., erases that too and prints Loading.. and so on. So I tried using \r, but python interprets it as \n. Is there a charcter I can use instead of \r?
I've tried using \b instead, but Python doesn't recognize it, either. For example, if I print qwerty\buiop, it just prints qwertyuiop.
This is the code I tried, using carriage return:
import time
for y in range (5):
for i in range (4):
print("Loading","."*i, end="\r")
time.sleep(0.5)
However, instead of printing how I want it to, it prints like
Loading
Loading .
Loading ..
Loading ...
in different lines.
How do I solve this problem? Is there a different character I can use?
I'm using IDLE and MacOS.
Thank you so much!
I know you probably know an answer by now, but for people still looking for one:
Try using an empty end, while using \r at the beginning of each print to overwrite the previous one.
import time
for i in range (4):
print("\rLoading","."*i, end ="")
time.sleep(0.5)
Try running your program in terminal.
Different shells interpret the \r character in different ways. A lot of them will write out each print as a separate output rather than one ongoing stream. Terminal should interpret it the way you are thinking
This question already has answers here:
How to create an 'empty if statement' in python
(4 answers)
Closed 5 years ago.
Let's say I have some if statements:
thing = true
if thing:
#something i will decide later
else:
print ("well that thing is not true")
If I am just leaving something there for testing or want to have it there for structural purposes or maybe it just makes sense with the rest of the code (like an if statement for each letter, but "Z" is empty, and i might add it later).
In Python, I absolutely CANNOT leave an if statement empty. Why is that? Is it something to do with the indentations and the getting confused? Is it because there are no brackets, so then somehow it will get messed up? I don't understand why I this is disallowed. Please explain. Thanks!
you can leave if statement empty by using pass statement like below:
if thing:
pass
pass statement does nothing but act as a placeholder.
That is because blank spaces means a great deal to python unlike other languages like C++ and java. Python uses blank spaces to structure code and define code blocks and scopes. If you want to keep it blank just write pass and python will not care about the if statement
You need to have a pass keyword in your if block.
See this SO answer for why: https://stackoverflow.com/a/22612774/4227970
As it has already been mentioned you can use pass if you want to have an empty if statement or empty function body. However, you can also use negated if statement if you need to:
if not thing:
print ("well that thing is not true")
It will make your code shorter.
Three characters shorter than Singh's:
if thing:
0
This question already has answers here:
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 6 years ago.
I think the piece of code below is written in python 3 and my python 2 cannot run it. There is some problem with 'end'. How could I fix it? I don't know what is the logic behind end an i am very new to python
Any help much appreciated!
def myPrint(itp):
for i in range(10):
print("**",end=="")
for j in range(10):
print(itp[i][j],"**",end=="")
print()
You have two errors in your code, you need to replace both end=="" with end="". You are not supposed to compare the parameter end with an empty string, but you want to end the printing with an empty string, hence, do an assignment to the parameter end.
In Python 3.x, the end='' part will place a whatever parameter end is assigned with (here, an empty string) after the displayed string instead of a newline.
If you want to have the print functionality of python3 in python2, simply do an import :
from __future__ import print_function
(I am assuming the double == after end is a typo. end as a kwarg determines the end of line; the code likely originally was print("**",end="") with one = character)
To get print function semantics, you can set the __future__ flag print_function, by starting your file with
from __future__ import print_function
Be aware that the changes required to run a full Python 3 program under Python 2 are far more extensive than simply enabling some future flags (while you're add it, consider enabling unicode_literals). By far the easiest way to run a Python 3 program is to install a Python 3 interpreter.
This question already has answers here:
How to print one character at a time on one line?
(4 answers)
Closed 8 years ago.
Ok this is going to be hard to ask/explain but bear with me.
Im trying to just make things look cool when I use the while True command.
What I am trying to do Is make it type stuff slower or one letter at a time.
For example, here is my code.
while True:
print ("010101010101010101010101010101")
print ("010101010101001010101010101010")
print ("010101010101010101010101010101")
When I do that it obviously rapidly repeats the commands I entered in the file.
I am aware there is the following,
import time
time.sleep(5)
But I wan't it to type it one at a time, not on a 5 second relay.
I hope you can understand what I am trying to ask. Thank you so much for helping me.
Here's one possibility:
import sys
import time
def cool_print(str):
for char in str:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(0.05) # Or whatever delay you'd like
print # One last print to make sure that you move to a new line
Then instead of print ("010101010101010101010101010101"), you'd use cool_print("010101010101010101010101010101").
It sounds like you want a delay between each actual character, so you need to call sleep in between each one:
import time
while True:
for binary_char in "10101010101010101":
time.sleep(5) # Replace this with a much smaller number, probably
print binary_char, # Remove trailing comma to print each character on new line