I am writing a program that is suppose print a number that corresponds to its factor, rows by columns, otherwise known as a multiplication table. The first row and column each start with 0, then go to 10. Each number on each row is multiplied by each number on each column. While my program is successful in generating each number, it is not lined up. The reason for this is because on numbers with more than one digit, take up more space (10 is not lined up with 9 because of space, affecting other numbers lineage).
I having tried many things, one including adding more space to each row and column.
Here is some of my code:
t = 1
row = ""
for i in range(11):
i = str(i)
row += i
row += " "
print(row)
row = ""
for i in range(1, 11):
row = ""
for i in range(1, 11):
i *= t
i = str(i)
row += i
row += " "
i = int(i)
i /= 10
print("%0.0f" % i, "", row)
t += 1
If you use a recent Python (>3.7 I think) you can use the "f-string" to right-align numbers:
for i in range(1, 11):
for t in range(1, 11):
print(f"{i*t:>5}", end="")
print()
which gives
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Related
Need help creating a 9 column descending half pyramid.
The first column must count 1-9.
Then with each row they should continue counting with that starting multiple. Would appreciate any help please.
for num in range(10):
for i in range(num):
print (num, end=" ")
print("\n")
>Current output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
>I need it to output as:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 27 35 42
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
You got your inner looping wrong. Considering the outer loop represents line numbers, the inner loop should start from line number, incrementing each time by line number till the square of line number:
for num in range(1, 10):
for i in range(num, num*num+1, num):
print(i, end=" ")
print("\n")
# 1
# 2 4
# 3 6 9
# 4 8 12 16
# 5 10 15 20 25
# 6 12 18 24 30 36
# 7 14 21 28 35 42 49
# 8 16 24 32 40 48 56 64
# 9 18 27 36 45 54 63 72 81
you were almost there! just a few minor adjustments:
for mul in range(1, 10):
for i in range(1, mul+1):
print (i * mul, end=" ")
print("\n")
what you need to print is i * mul; and the range needs to start at 1 and stop at (i.e. one before) mul+1.
a bit more compact and neatly aligned:
for mul in range(1, 10):
print(' '.join(f'{mul*i:2d}' for i in range(1, mul+1)))
this outputs:
1
2 4
3 6 9
4 8 12 16
...
9 18 27 36 45 54 63 72 81
This question already has answers here:
Python Print parameter end
(3 answers)
Closed 3 years ago.
I'm trying to create a table that looks like this:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
I was able to get it done by doing this:
for x in range(0, 50, 10):
print(x, x + 1, x + 2, x + 3, x + 4, x + 5, x + 6, x + 7, x + 8, x + 9)
But it seems like this definitely isn't the most efficient way to do it. I've tried adding:
# for y in range(1, 10):
# print(x, x + y)
after the first for loop in my original code, but it doesn't work.
Thank you!
You can use a for loop and join a list comprehension for each row to print your table.
for i in range(0, 50, 10):
print(' '.join([str(j) for j in range(i, i + 10)]))
Similarly, you can unpack a row as a list into the print method's parameters.
for i in range(0, 50, 10):
print(*[j for j in range(i, i + 10)])
You can specify the end parameter of the print() function:
for x in range(50):
if (x+1)%10 == 0:
end_char = "\n"
else:
end_char = " "
print(x,end=end_char)
OUTPUT:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
The print() function add a new line at the end of each statement by default, but you can modify how the string should end: "\n" (new line) if the next number is divisible for 10, " " (a space) otherwise
I am supposed to write a code that results in this:
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
and this is my code:
for i in range(10):
print("0", end=" ")
print("")
for i in range(0, 10):
print(i, end=" ")
print("")
for i in range(10):
i = i * 2
print(i, end=" ")
print("")
for i in range(10):
i = i * 3
print(i, end=" ")
print("")
for i in range(10):
i = i * 4
print(i, end=" ")
print("")
for i in range(10):
i = i * 5
print(i, end=" ")
print("")
for i in range(10):
i = i * 6
print(i, end=" ")
print("")
for i in range(10):
i = i * 7
print(i, end=" ")
print("")
for i in range(10):
i = i * 8
print(i, end=" ")
print("")
for i in range(10):
i = i * 9
print(i, end=" ")
print("")
I just coded a bunch a 'for' statement and I feel like there would be a way that I could code where I don't have to be too repetitive. I was wondering if there is a more efficient way to do it? And how do I get the right alignment?
Thank you.
All you need to do is run a loop from 0 to 9. This loop variable will be the "multiplier". At each iteration, multiply the multiplier with numbers 0 to 9. As shown below, I've done this with map + int.__mul__, but there are other (possibly faster ways, if performance is your concern) of doing this.
To justify, you can use str.format with right justification ({:>3}.format(number)).
for i in range(10):
print(*map('{:>3}'.format, map(i.__mul__, range(10))))
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
I have to make a times table code using recursive functions. I have to ask the user for a number and print out the times tables from 1 to 12. And I have to use recursive functions and it is not allowed to use for loops or while loops and all variables besides the user input have to be defined inside the functions. I am having trouble defining the number that the user provided number needs to be multiplied with. E.X. 2 x 1 2 x 2 2 x 3.
def times_tables(num):
def multiply(x):
product = x * num
if x < 12:
print (str(multiply(x + 1)))
user = input("Enter a number: ")
times_tables(user)
If I define x in the times_tables function then every time the function runs it will get set back to whatever I set it to the first time. Thanks for your help.
You are not modifying x, x is passed by value, this mean it is copied.
If you want to keep the exit conditon outside the recursion you need a way to write X directly from the recursion, which probably would involve a global (bad practices so avoid).
You need to have the exit condition inside multiply, because that will be your recursion, in that case your X will increase and you will do the check on the proper incremented value. Or change the function all together as ruggfrancesco suggested
def times_tables(n, t=1):
if t == 13:
return
print(str(n) + " x " + str(t) + " = " + str(n*t))
times_tables(n, t+1)
times_tables(int(input("Enter number: ")))
Enter number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
When I (Image) Google "times table", I get a very different result from what the other answers produce. Below is my recursive solution in two dimensions:
def times_table(limit):
number_format = "{{:{}}}".format(len(str(limit ** 2)))
def times_table_recursive(number, increment):
minimum = max(increment, 1)
if number <= minimum * limit:
print(number_format.format(number if number > 0 else 'x'), end=' ')
times_table_recursive(number + minimum, increment)
elif increment < limit:
print()
increment += 1
print(number_format.format(increment), end=' ')
times_table_recursive(increment, increment)
else:
print()
times_table_recursive(0, 0)
times_table(12)
OUTPUT
> python3 test.py
x 1 2 3 4 5 6 7 8 9 10 11 12
1 1 2 3 4 5 6 7 8 9 10 11 12
2 2 4 6 8 10 12 14 16 18 20 22 24
3 3 6 9 12 15 18 21 24 27 30 33 36
4 4 8 12 16 20 24 28 32 36 40 44 48
5 5 10 15 20 25 30 35 40 45 50 55 60
6 6 12 18 24 30 36 42 48 54 60 66 72
7 7 14 21 28 35 42 49 56 63 70 77 84
8 8 16 24 32 40 48 56 64 72 80 88 96
9 9 18 27 36 45 54 63 72 81 90 99 108
10 10 20 30 40 50 60 70 80 90 100 110 120
11 11 22 33 44 55 66 77 88 99 110 121 132
12 12 24 36 48 60 72 84 96 108 120 132 144
>
Only goes up to times_table(30) without expanding Python's recursion depth limit.
def fun (no,i=1):
if i==10:
print (no*i)
else:
print (no*fun(i+1)
no=int (input ("Enter a no="))
fun (no)
I am trying to teach myself python using interactivepython.org. I have come across a problem that I can not figure out. I have the slope and the spacing correct. I need it to print one less number every time. Could anybody help a newbie out?...
My Code:
for j in range(11):
for i in range(j):
print(str(i), end=" ")
print()
print("")
Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
Desired Output:
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
The exercise is about nesting for loops...I know there are other ways to do this.
This should do it:
start = 10
width = 9
for i in range(1, width+1):
for _ in range(i):
print (start, end=" ")
start += 1
print('\n')
Output:
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
Well this one again for you Brandon Shockley :)
code:
x = 9
lines = 10
for i in range(lines):
for j in range(i):
x+=1
print x,
print ''
output:
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 5
Hope This helps :)
You can do it like this
current, levels = 10, 9
for i in range(levels):
for j in range(i + 1):
print(current, end = " ")
current += 1
print("\n")
Output
10
11 12
13 14 15
16 17 18 19
20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36 37
38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54
>>> lst = list(range(54,9, -1))
>>> for j in range(11):
... for i in range(j):
... if len(lst):
... print(lst.pop(), end=" ")
... print(" ")
inc = 10
for j in range(10):
for i in range(j):
print(inc, end=" ")
inc += 1
print()
print("")
Is anything wrong with this?
Single loop, in Python 2.x (can't remove the space after each print)
c = 1
j = 0
for i in range(10, 55):
print str(i) + ',',
j += 1
if j == c:
print
c += 1
j = 0
Using join
start = 10
for i in range(1, 10):
print(' '.join(map(str, range(start, start + i))))
start += i
A pointlessly compact version using join and some maths:
print('\n'.join(' '.join(map(str,
range(10 + i * (i+1) / 2, 10 + (i+1) * (i+2) / 2))) for i in range(9)))
And confusingly, this also works (in python 2):
j = 9
for i in range(10):
for j in range(j + 1, j + i + 1):
print j,
print