for index in range(0, len(order_of_fruits)):
maze[prevY][prevX] = ' '
curr = order_of_fruits[index]
maze[curr[1]][curr[0]] = 'P'
prevX = curr[0]
prevY = curr[1]
result_maze = ""
for i in range(len(maze)):
for j in range(len(maze[0])):
result_maze = result_maze + maze[i][j]
result_maze = result_maze + '\n'
animation.append(result_maze)
#animate
for index in range(0, len(animation)):
time.sleep(0.2)
sys.stdout.write("\r" + str(animation[index]))
sys.stdout.flush()
Hi, my problem is that I have a two-dimentionay array whose condition will update. Then I convert each updated array to a string and append each string to a list which are used to print in the console. Now I want print the changing condition of this maze which have already been converted to string in place. I used the "\r" and flush but it does not work. I guess this might because I have "\n" in each of my string. So is there any way that I can print the series of maze in console in place? So the result looks like only one maze appears on the console whose condition will update every 0.2?
Thanks!
"\r" maybe put in the end of line.
import time
for index in range(0, len(animation)):
sys.stdout.write(str(animation[index]) + "\r")
sys.stdout.flush()
time.sleep(0.2)
You might consider using the curses module. This program updates the screen, waits 0.2 seconds, and updates the screen again:
from curses import wrapper
import time
def main(stdscr):
stdscr.clear()
for i in range(5):
for y in range(5):
for x in range(5):
stdscr.addstr(y, x, chr(ord('1')+i))
stdscr.refresh()
time.sleep(0.2)
stdscr.getkey()
wrapper(main)
Related
I've made a program that makes a random change to a number every 10th of a second, and I want the new number to be printed in the same line as before. However, it just prints nothing when I add "end = '' " to the print command.
Here's my code:
import random, time
stock = 1000000
while True:
change = random.randint(0, 10)
operation = random.randint(0, 1)
if operation == 0:
stock -= change
else:
stock += operation
print(stock, change, end = ' ')
time.sleep(0.1)
I'm guessing you would want to use
print(stock, change, end = '\r')
add flush=True and sep=' ' in your print function. That will make sure the print dynamically in one line.
e.g
print(stock, change, sep=' ', end='', flush=True)
I am writing a small python3 script that prints a line in a for loop. after the line is printed, it clears that line and prints another with the percentage going up by one. However, I am struggling to get the for loop to print on two seperate lines at the same time.
If you are unclear about anything or have any questions. Run the script, hopefully you'll see what I mean.
import time
import sys
import os
# you dont need this, it merely clears the terminal screen
os.system('clear')
for c in range(1, 101):
print("xaishfsaafsjbABSF - " + str(c) + "% |======= |")
sys.stdout.write("\033[F") # goes back one line on terminal
sys.stdout.write("\033[K") # clears that line
time.sleep(0.03)
if c == 100:
print("xaishfsaafsjbABSF - " + str(c) + "% | COMPLETE |")
I have tried adding for z in range(1, 3): above for c in range(1, 101): but that just prints the second one after the first one. I want them to print at the same time on different lines.
Using ANSI escape codes to move the curser up 2 lines:
import time
for i in range(1,21):
print("Number:",i)
print("Percent:",i*100/20)
time.sleep(0.5)
print("\033[2A", end="") # move up 2 lines
print("\033[2B", end="") # move down 2 lines after the loop
Just use \r:
import time
for c in range(1, 101):
print("xaishfsaafsjbABSF - " + str(c) + "% |======= |\r", end="")
time.sleep(0.03)
if c == 100:
print("xaishfsaafsjbABSF - " + str(c) + "% | COMPLETE |")
My idea was to insert a line of slow-moving text into a set of printed text after the printed text had finished printing out.
So for example:
##############
# SlowText #
##############
The border would be printed out immediately and then the SlowText would appear after a short period, writing out slowly.
I've tried a couple of different slow-moving text snippets to perform the actual writing. like:
def print_slow(txt):
for x in txt:
print(x, end='', flush=True)
sleep(0.1)
and
def insertedtext():
text = " E..n..j..o..y..."
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
I've tried using '#'+ words + '#' , tried throwing another print("words") in there. Heck, I even attemped at making it a variable but since I am quite new to Python I just can seem to figure it out or google it correctly enough to find it on my own. Any/all help appricated.
I think this is what your looking for :
from time import sleep
def make_box (character, width) :
print (character * (width + 4))
print (character + ' ' * (width + 2) + character)
print (character * (width + 4))
def print_slow (character, text):
print ('\033[2A' + character, end = ' ')
for x in text:
print (x, end='', flush=True)
sleep (0.1)
print ()
box_character = '#'
text = 'This is a test'
make_box (box_character, len (text))
print_slow (box_character, text)
The line print ('\033[2A... moves the cursor up two lines.
I want to store all the "o"'s printed by stdout.write function into a a variable which could be accesable any time
I have tried using len function to break loop once it reaches certain amount of strings, but no luck
import time
import sys
while True:
sys.stdout.write("o")
sys.stdout.flush()
time.sleep(0.05)
Very simply, you can add them to a string, one at a time:
record = ""
while True:
sys.stdout.write("o")
sys.stdout.flush()
record += 'o'
time.sleep(0.05)
A slightly faster way is to count the quantity written, and then produce your desired string:
count = 0
while True:
sys.stdout.write("o")
sys.stdout.flush()
count += 1
time.sleep(0.05)
# Insert your exit condition
record = 'o' * count
Keep on appending values to a string. Check the length and break when desired.
import time
import sys
data = ""
while True:
temp = "o"
data += temp
sys.stdout.write(temp)
sys.stdout.flush()
time.sleep(0.05)
if(len(data)==10):
break;
You could keep track of the number of O's in a separate variable:
number_of_os = 0
while True:
sys.stdout.write("o")
sys.stdout.flush()
number_of_os += 1
if number_of_os >= 100:
break
time.sleep(0.05)
I am new in python. I have a for loop inside which I have if ...: condition.
I want to print out the (list) of items which went through the for loop.
Ideally, items should be separated by spaces or by commas. This is a simple example, intended to use with arcpy to print out the processed shapefiles.
Dummy example:
for x in range(0,5):
if x < 3:
print "We're on time " + str(x)
What I tried without success inside and outside of if and for loop:
print "Executed " + str(x)
Expected to get back (but not in list format), maybe through something like arcpy.GetMessages() ?
Executed 0 1 2
phrase = "We're on time "
# create a list of character digits (look into list comprehensions and generators)
nums = [str(x) for x in range(0, 5) if x < 3]
# " ".join() creates a string with the elements of a given list of strings with space in between
# the + concatenates the two strings
print(phrase + " ".join(nums))
Note. A reason for the downvotes could help us new users understand how things should be.
Record your x's in a list and print out this list in the end:
x_list = []
for x in range(0,5):
if x < 3:
x_list.append(x)
print "We're on time " + str(x)
print "Executed " + str(x_list)
If you use Python3 you could just do something like this..
print("Executed ", end='')
for x in range(0,5):
if x < 3:
print(str(x), end=' ')
print()