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 |")
Related
I have written for a coding problem assigned to me essentially there is a scratch and win ticket and I have to figure out what prizes I could win. I will be given 10 inputs and each output after running through should be on its own line. However Every time I run the code with 10 inputs all of the inputs just appear on one line.
How do I make sure that each output appears on its own line?
Although to be noted my code does output the correct answer its just the formatting that is troubling I want each answer to be on its own line instead of just being on one line.
from collections import Counter
def Winner(L):
zz = Counter(L)
if zz["63"] == 9:
print("No Prizes Possible")
elif zz["63"] == 0:
for T in zz:
if zz[T] >= 3:
print("$" + T, end=" ")
else:
for V in zz:
KK = zz[V] + zz["63"]
if KK >= 3:
if V != "63":
print("$" + V, end=" ")
for d in range(10):
z = [input() for i in range(9)]
yy = []
for g in z:
if g == "?":
yy.append(ord(g))
else:
yy.append(g.replace("$", ""))
yy = [int(i) for i in yy]
yy.sort()
yy = [str(i) for i in yy]
Winner(yy)
Here is a sample input of what I mean:
$10
$100
?
$10
$1
$50
$50
$1000
$1
essentially having 10 inputs like these.
If you add \n (the newline character) at the end of your string, you will add a newline after each time your program prints. For example, you could have this loop which would print "hello world" 10 times, each on its own separate line.
for i in range (10):
print('hello world', '\n')
Or more conveniently, you can get rid of the end = " ", and Python will automatically add a new line.
You are using end=" " in your print function.
Just leave it as print("$" + T) and print("$" + V) so it appends newline by default.
So I have the data to output like this for example
progress 1: *
progress-moduletrailer 4: ****
do_not_progress 6:******
exclude 2: **
But I would want it to show it like this
progress
2:
**
etc
I would appreciate any help with this, very stuck.
print("Progress",Progress, ":", end= " ")
for i in range (Progress):
print("*", end = " ")
print("\n")
print("Progress_module_trailer",Progress_module_trailer, ":", end= " ")
for i in range (Progress_module_trailer):
print("*", end = " ")
print("\n")
print("Do_not_progress_module_trailer",Do_not_progress_module_trailer, ":", end= " ")
for i in range (Do_not_progress_module_trailer):
print("*", end = " ")
print("\n")
print("Exclude",Exclude, ":", end= " ")
for i in range (Exclude):
print("*", end = " ")
print("\n")
print(Progress+Progress_module_trailer+Do_not_progress_module_trailer+Exclude,"Number of students in total")
Try defining the separator argument of the print function and using an f-string format as such:
print("Progress", f"{Progress}:", sep='\n'))
FYI: The default separator is a single space, changing it to a new line (or 2 new lines if you so wish) can be done through each function call.
If I understood it correctly a \n between progress, the number and the stars should do the trick!
The \n means that a new line is started.
Example:
print(Hello world)
Prints out:
Hello world
but
print(Hello\nworld)
Prints out:
Hello
World
My desired output is two half pyramids separated by two spaces.
length = int(input("Enter size of pyramid."))
hashes = 2
for i in range(0, length):
spaces = length - (i+1)
hashes = 2+i
print("", end=" "*spaces)
print("#", end=" "*hashes)
print(" ", end="")
print("#" * hashes)
However, this ends up printing only the first hashes of each row on the left pyramid. If I get rid of the end= in line 7, the pyramids are both printed correctly, but with newlines after each row. Here are the outputs:
With end=:
# ##
# ###
# ####
# #####
Without end=:
##
##
###
###
####
####
#####
#####
All I want now is to have the second output, but without the newlines.
The most straightforward way to print any output you want without newlines is to uses sys.stdout.write. This writes a string to the stdout without appending a new line.
>>> import sys
>>> sys.stdout.write("foo")
foo>>> sys.stdout.flush()
>>>
As you can see above, "foo" is written with no newline.
You're multiplying the end parameter by the number of hashes, instead of multiplying the main text portion.
Try this modification:
length = int(input("Enter size of pyramid."))
hashes = 2
for i in range(0, length):
spaces = length - (i+1)
hashes = 2+i
print(" " * spaces, end="")
print("#" * hashes, end="")
print(" ", end="")
print("#" * hashes)
Try this algorithm:
length = int(input("Enter size of pyramid."))
# Build left side, then rotate and print all in one line
for i in range(0, length):
spaces = [" "] * (length - i - 1)
hashes = ["#"] * (1 + i)
builder = spaces + hashes + [" "]
line = ''.join(builder) + ''.join(builder[::-1])
print(line)
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)
I wrote this -
import time
timeout = time.time() + 2
dot, i = ".", 0
while timeout > time.time():
print ("Loading" + dot*i, end = "\r")
time.sleep (0.1)
if i == 3:
i = 0
else:
i += 1
It runs fine till "loading...", then I need it to go back to "loading" and loop till the time expires.
PS - I'm running this on Windows Powershell.
After the three dots you return and only draw 1. But the 2 previous ones remain!
Just include (3-i) whitespaces after the dots
print ("Loading" + dot*i + ' '*(3-i), end = "\r")
By the way, a sleep of only 0.1 will make the dots very fast, try some bigger sleep for better visualization
Your loop is continuing the way it should, but the periods from previous iterations aren't being erased or overwritten.
Printing some spaces after the periods should overwrite any text from the previous iteration:
print("Loading" + dot*i + " ", end = "\r")