Overwriting printed statements in the IDLE in Python [duplicate] - python

This question already has answers here:
How to output to the same line overwriting the previous line?
(4 answers)
Closed 3 years ago.
I am looking for a method so that when something is printed continuously, it overwrite what the previous printed statement says. for example, if I am making a counter, normally the outcome in the IDLE would be: 1 2 3 4...., but however I'm looking to rewrite/overwrite what the previous printed statement says so it say "1" for a second then "2" appears but we can no longer see "1". Any suggestions? Sorry about how wordy this question is, I was having a hard time trying to write this where another person understands.

import time
arr = [1,2,3,4]
for element in arr:
print(element, end='\r')
time.sleep(1)
end='\r' in the print statement does the trick

Related

Python print statement dependent on lines following it? [duplicate]

This question already has answers here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Python print immediately?
(1 answer)
Closed 2 years ago.
I have the following code:
for file_name, content in corpus.items():
print('here')
content = [list(filter(lambda index: index not in remove_indices, content))]
corpus[file_name] = np.array(content).astype(np.uint32)
Where corpus is a 800,000 long dictionary with string keys and array values.
Things were taking forever so I decided to check how fast each iteration was by adding in that print statement.
If I comment the last two lines out it prints lots of heres really fast, so there's no problem with my iterator. What's really weird is that when I uncomment the last two lines, here takes a long time to print, even for the first one! It's like the print statement is somehow aware of the lines that follow it.
I guess my question speaks for itself. I'm in Jupyter notebook, if that helps.

How to change time in available line in python [duplicate]

This question already has answers here:
How to rewrite output in terminal
(4 answers)
Closed 2 years ago.
Let's assume there is a code which is using with anaconda prompt. While the program flowing, every second is printing to screen. However, here every second is printed the same or next line.
I want to print every second in the same place.
For example, print 30 and after 1 second later, delete 30 and print 29 to the same place.
How can I do that with python?
You can use \r to return to the start of line instead of moving the cursor to the next line (\n). Whether or not this works with your shell is an open question; e.g. IDLE occasionally has problems with this.
import time
for x in range(10):
print(x, end='\r')
time.sleep(1)
print() # to add a newline, finally
print('The end!')
After every second when you have to clear the screen you can use this :
import os
os.system('cls')

how to get out of nested loop and continue with iteration of outer loop [duplicate]

This question already has answers here:
Exit while loop in Python [duplicate]
(6 answers)
Closed 3 years ago.
Write a python function, find_ten_substring(num_str) which accepts a string and returns the list of 10-sub strings of that string.
A 10-sub string of a number is a sub string of its digits that sum up to 10.
sample input='3523014'
actual output=['5230', '23014', '523', '352']
i have tried the below code it is printing only one sub string that adds up 10 (only [28])and then terminating.
def find_ten_substring(num_str):
sumi=0
list1=[]
substr=''
for i in range(0,len(num_str)):
for j in range(i,len(num_str)):
sumi=sumi+int(num_str[j])
substr=substr+num_str[j]
if(sumi==10):
list1.append(substr)
print(list1)
break
sumi=0
substr=''
continue
num_str="2825302"
print("The number is:",num_str)
result_list=find_ten_substring(num_str)
print(result_list)
You specifically told it to quit as soon as it found that one solution. Look at the bottom of your outer loop:
sumi=0
substr=''
break
This resets the accumulation variables, but then breaks the loop rather than repeating. Remove the break and return to your code development -- you have other errors, starting with lack of any return value.
Also, you should learn basic debugging. For starters, a few print statements inside your code can trace the data and control flow. See this lovely debug blog for help.

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

Python overwrite new line not behaving as expected [duplicate]

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 9 years ago.
I am trying to output text to stdout, overwriting the previous text, for example
for i in range(12):
print i
but with i replacing the previous value each time rather than appearing on a new line
From looking at quite a few previous posts with similar questions it seems that there are a few ways of doing this, possibly the simplest being (for Python 3.x on)
for i in range(12):
print(i,end="\r")
sometimes with a comma at the end of the print statement, sometimes not. However, without the comma I get no output at all and with the comma I get
(None,)
(None,)
(None,)
...
Is this something related to my terminal perhaps? I get similar results no matter which of the previous posted solutions to the problem I try.
Thanks for any help!
If you do this in a terminal you won't see any output because the for loop finishes too fast for you to see the output changing. After the loop is done the terminal's prompt overwrites the output.
Try this instead:
>>> for i in range(9999999):
... print(i, end="\r")
I'm surprprised you don't get a syntax error when omitting the comma.

Categories