'How to print in a single line without using end =''
I have a task to create a row of numbers. what I have now works and prints on one line. I want to try to use it without end. What I've tried already is creating a variable new_line = "" and adding that / equaling that to the string in the print line. I also need to be able to print another "|" at the end of the string only and I can't do that with end.
def print_row(n, max_power, column_width):
count = 0
exponent = max_power
max_power = 0
while count < exponent:
max_power = max_power + 1
value = power(n, max_power)
print("|",padded(value, column_width),end='')
count = count + 1
Solution 1:[1]
You're using the loop to control the wrong thing. Use the loop to build a list of values to be combined using | as a separator, then print that string with one call to print.
def print_row(n, max_power, column_width):
values = [padded(power(n, p), column_width) for p in range(max_power)]
print("|" + "|".join(values), end="|")
The initial "|" can be added to the value to actually print (as shown), or could be output with a preceding call to print("|", end='').
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
