print down the line and automatically updated continuouslyin [duplicate] - python

This question already has answers here:
Python 3 Print Update on multiple lines
(2 answers)
Closed 1 year ago.
i have a command
import time
i=0
b=0
while(True):
i=i+1
b=b+1
time.sleep(2)
print('Jack: {} '.format(str(i)) ,end='')
print('Han: {} '.format(str(b)) ,end='\r')
i want print this like
Jack: 1
Han : 1
in the next loop it still prints at that position only the number changes like this
Jack: 2
Han : 2
Note: Do not combine 2 prints into 1 print
I need ideal

If I read what you want correctly, you want to update the number, not print a new line. In which case use blessings and print at the location on the screen instead.
Something like:
from blessings import Terminal
from time import sleep
term = Terminal()
i = 0
while True:
print(term.move(1, 1), f"I am a number: {i}")
i += 1
sleep(1)
Note that there are other terminal control libraries, I just like blessings.

Related

Can I continue an input after a new line is created

I want to make my input continue after a new line is created in the terminal.
Here's the quick script I wrote:
import threading, time
def inn():
while True:
input()
def count():
a=0
while True:
a+=1
print(a)
time.sleep(1)
t1 = threading.Thread(target=inn)
t2 = threading.Thread(target=count)
t1.start()
t2.start()
Is there any way I could accomplish this, preferably with in-built functions.
if you change your code to:
.
def inn(): # in is restricted name in python
while True:
print(input())
.
.
.
you will notice that your code already does what you want! Even though it doesn't look like so, the input is not interrupted, take a look at the program:
1
2
t3
his 4
iss 5
my 6
strin7
g 8
this iss my string
9
The only change your code needs is to change restricted name in.

How do I loop my python code a specific number of times?

I've been struggling to loop my code a specific number of times. I've YouTubed mulitple videos on while statements for looping but it never works. I have gotten to a point where I managed to run the code but then once it ran once it started looping and listing number 1 - 10. I'm assuming this is because I specified <10 to loop. I don't think I'm understanding but I am a visual learner and the text examples aren't helping. Here's the code I would like to loop.
import urllib.request
import os
urllib.request.urlretrieve("https://websitewithimageonit.com/", "")
i = 0
while os.path.exists("image%s.jpg" % i):
i += 1
fh = open("image%s.jpg" % i, "w")
I tried to do it myself and used a while loop like this below. The code ran, saved 1 image but then just listed 1 - 10 in the PyCharm console.
import urllib.request
import os
import time
urllib.request.urlretrieve("https://websitewithimageonit.com/", "")
i = 0
while os.path.exists("image%s.jpg" % i):
i += 1
fh = open("image%s.jpg" % i, "w")
condition = 1
while condition < 10:
print(condition)
condition += 1
After it runs the code it prints this in the console
1
2
3
4
5
6
7
8
9
Process finished with exit code 0
Which I'm assuming is me messing up the while loop. Where am I going wrong? Thanks
I think you wanted to do something like:
i = 0
while os.path.exists(f"image{i}.jpg"):
with open(f"image{i}.jpg", "w") as fh:
#do stuff with the file here
#do other stuff here
#at the end of the loop, increment i
i += 1
Read the following for more info
https://www.geeksforgeeks.org/with-statement-in-python/
https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/
You can combine for loop with range function
so if you want to do 50 times something here is the loop
for times in range(50):
print("x")

Python-3.x time.sleep() wont work for print to file [duplicate]

This question already has answers here:
How can I flush the output of the print function?
(13 answers)
Closed 2 years ago.
I would like to code a python that adds every x seconds a value.
But when I add the time.sleep(x) theres no output into the .txt file anymore.
And is there a way to stop the loop after a certain time?
Code:
f = open('Log.txt','a')
while True:
print("This prints once a second.", file=f)
time.sleep(1)
Maybe something like this:
import time
while True:
with open('Log.txt','a') as f:
f.write("This prints once a second.\n")
time.sleep(1)
If you want to stop after a given time:
import time
stop_after = 10 # seconds
start = time.time()
while time.time() - start < stop_after:
with open('Log.txt','a') as f:
f.write("This prints once a second.\n")
time.sleep(1)
It is best to use a context manager, and this works:
import time
while True:
with open('Log.txt','a') as f:
print("This prints once a second.",file=f)
time.sleep(1)

Trying to make flash cards in Python 3 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I just recently started with python and was trying to make some sort of flash cards. I did this by making a text file inside note pad and just writing some simple math problems. The problems were written like this.
1 + 1 = ???
2
2 + 2 = ???
4
8 x 4 = ???
32
then my code was this.
#!/usr/bin/python3
x = 0
f=open('cardsss.txt').readlines()
while x < 6:
line = f
print(line[x])
answer = input()
if answer == line[x+1]:
print ('Correct')
else:
print ('Wrong')
x = x + 2
print ("Done")
The problem is that when i put the answer in, it always says that what ever i put in is wrong, and i can not figure out why.
Where i would get a screen like this
1 + 1 = ???
2
Wrong
2 + 2 = ???
4
Wrong
8 x 4 = ???
32
Wrong
Done
The lines containing the answers end with a new line character \n. You need to strip the new line character off the lines you're reading from the file to make the items match:
if answer == line[x+1].strip():
...
Solution:
TESTS_NUM = 3
with open('cardsss.txt') as f:
for _ in range(TESTS_NUM):
line = next(f)
print(line)
answer = input("Your answer: ")
right_answer = next(f)
if answer.strip() == right_answer.strip():
print("Correct")
else:
print("Wrong")
print("Done")
This solution works if the file 'cardsss.txt' doesn't contain empty lines.

python - print iteration number without new line [duplicate]

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed last month.
I have a loop:
for i in range(10):
print i
and it print :
1
2
...
8
9
OK
but I'm searching to make a unique line which actualize for each iteration like this :
for i in range(10):
magic_print "this is the iteration " + i + " /10"
with a result like:
"this is the iteration <i> /10"
<i> changing dynamically
Thanks !
As I understand your question, you would like to overwrite previous prints and count up. See this answer: https://stackoverflow.com/a/5419488/4362607
I edited the answer according to PM 2Ring's suggestions. Thanks!
import sys
import time
def counter():
for x in range(10):
print '{0}\r'.format(x),
sys.stdout.flush()
time.sleep(1)
print
counter()
the solution is the format function from the str object
for i in range(10):
print "this is the iteration {} /10".format(i)

Categories