I would assume it's due to buffering. Try adding flush=True as one of the optional parameter to print.
When using print, a newline character is added to your string unless you're overriding it (as you do) with the end parameter. For performance reasons, there exists an I/O buffer that only prints when it encounters a newline or the buffer fills up. Since your string doesn't contain a newline character anymore, you have to manually flush the buffer (i.e. send it to be printed).
from time import sleep
hash = '#'
while True:
print('\r' + hash, end='', flush=True)
hash = hash + '#'
sleep(1)
Python 3.9, print(file1.read) with no output - Stack Overflow
Why won't my python code print anything? - Stack Overflow
Python 3.9 No Output to Console
Syntax error on print with Python 3 - Stack Overflow
Videos
You can use enumerate to get that serial numbers starting from 1.
To print the output the way you mentioned you need to use String formatting - Docs
I have used {item:25} - This will make the item to be of width 25. You can put whatever number you wish to get desired space.
From the Docs:
Passing an integer after the ':' will cause that field to be a minimum number of characters wide. This is useful for making columns line up.
with open('menu.txt', 'r') as f:
lines = f.readlines()
for i,v in enumerate(lines,1):
item, price = v.strip('\n').split(',')
print(f'{i}. {item:25}{price}')
Output
1. Cheese Burger 5.00
2. Chicken Burger 4.50
3. Fries 2.00
4. Onion Rings 2.50
5. Drinks 1.50
You should open file in read ("r") mode to use it without changing. ./Menu.txt means the file and .py file are in the same folder. You can write exact path instead of "./Menu.txt".
file = open("./Menu.txt", "r")
lines = file.readlines()
file.close()
num_of_line = 1
for line in lines:
res = line.split(", ")
print(num_of_line, end=". ")
print(f"{res[0] : <20}{res[1] : ^5}")
num_of_line += 1
In Python 3, print became a function. This means that you need to include parenthesis now like mentioned below:
print("Hello World")
It looks like you're using Python 3.0, in which print has turned into a callable function rather than a statement.
print('Hello world!')
Add the parenthesis:
print('Output After Training:')
If you need compatibility for code so works with Python 2 or 3, use:
from __future__ import print_function
at the start of the code file and then use Python 3 way of using print().
Learn more about print() in Python 3 at:
- https://www.python-course.eu/python3_print.php
The answer to your question is in the error... missing parentheses in calling to print
Python3 requires you to use parentheses when using print - i.e. print("text")
I have tried print( 'number = ', number, end='\r')
print( '\rnumber =', number, end='')
print( 'number =', number, '\r')
and several others. In all case, especially frustrating, the first one, I get a newline inserted. I want the output to be all on the same line with just an incrementing value.
What am I doing wrong?
TIA,
crkirkwood
Python is whitespace sensitive, but docstrings may prevent you from getting a proper error due to your indentation.
Looks like your code should be indented like this:
au = 1.49598e11 #astronomical unit in meters
rx = au * np.asarray([.5,.8,.2]) #x-comp separation vector
ry = au * np.asarray([2.6,9.1,3.7]) #y-comp separation vector
rz = au * np.asarray([.05,.1,.25]) #z-comp separation vector
def svec(x,y,z):
'''computes the magnitude and vector components of the distance between two particles.'''
#for loop to compute vector components of separation between two particles
rvec = []
for i in range(3):
if i < 2:
vx = x[i]-x[i+1]
vy = y[i]-y[i+1]
vz = z[i]-z[i+1]
rvec.append([vx,vy,vz])
if i == 2:
vx = x[0]-x[-1]
vy = y[0]-y[-1]
vz = z[0]-z[-1]
rvec.append([vx,vy,vz])
return rvec
comp1 = ['x-comp[m]','y-comp[m]','z-comp[m]']
r0 = rvec[0].insert(0,'particle 0->1')
r1 = rvec[1].insert(0,'particle 1->2')
r2 = rvec[2].insert(0,'particle 0->2')
print(tabulate(rvec,headers=comp1))
The indentation in your code sample seems off: you define a function but the function body is not indented. Can you make sure that the indentation in the pasted code matches what you're running?
This is important because if your print statement comes after the return statement in the function, it would explain why nothing is printed. (Unlike in some other languages, Python will not complain if you have unreachable code after a return statement.) But without seeing the actual indentation in your program, it's impossible for us to say.
EDIT: Now that the indentation has been fixed, I can confirm that the return statement will indeed prevent everything after it from being executed. Perhaps you meant to dedent all of the lines after the return statement?
You are almost certainly experiencing output buffering by line. The output buffer is flushed every time you complete a line, but by suppressing the newline you never fill the buffer far enough to force a flush.
You can force a flush using flush=True (Python 3.3 and up) or by calling the flush() method on sys.stdout:
for entry in categories:
print(entry, ", ", end='', flush=True)
You could simplify that a little, make , the end value:
for entry in categories:
print(entry, end=', ', flush=True)
to eliminate the space between the entry and the comma.
Alternatively, print the categories as one string by using the comma as the sep separator argument:
print(*categories, sep=', ')
Check categories isn't empty - that'd make it print nothing - also I'd consider changing your code to make use of the sep argument instead (depending how large categories is):
print(*categories, sep=', ')
eg:
>>> categories = range(10)
>>> print(*categories, sep=', ')
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Then you don't have to worry about flushing/trailing separators etc...