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
Related
I am trying to write a code where I put a number n in input() and in output I will get three same nxn tables. I cannot use if's nor '\t' nor lists. I am only a begginer so we cannot use any difficult functions or anything like that.
This is my code:
n = int(input('n: '))
for i in range(n):
for j in range(n):
print(f'{i*n + j + 1:2}', end=' ')
for k in range(n):
print(f'{i*n + k + 1:2}', end=' ')
for l in range(n):
print(f'{i*n + l + 1:2}', end=' ')
print()
The problem is that I need to put 4 whitespaces (a tab) behind every line of a table ( so that tables can be recognized as 3 and it doesn't look like one table).
My output looks like this now:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
6 7 8 9 10 6 7 8 9 10 6 7 8 9 10
11 12 13 14 15 11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 16 17 18 19 20 16 17 18 19 20
21 22 23 24 25 21 22 23 24 25 21 22 23 24 25
Output should look like this:
n: 5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
6 7 8 9 10 6 7 8 9 10 6 7 8 9 10
11 12 13 14 15 11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 16 17 18 19 20 16 17 18 19 20
21 22 23 24 25 21 22 23 24 25 21 22 23 24 25
Every time you finsh printing part of the line, mean after every for loop print some spaces:
n = int(input('n: '))
for i in range(n):
for j in range(n):
print(f'{i*n + j + 1:2}', end=' ')
print(' ', end='')
for j in range(n):
print(f'{i*n + j + 1:2}', end=' ')
print(' ', end='')
for j in range(n):
print(f'{i*n + j + 1:2}', end=' ')
print(' ', end='')
print()
Output:
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
6 7 8 9 10 6 7 8 9 10 6 7 8 9 10
11 12 13 14 15 11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 16 17 18 19 20 16 17 18 19 20
21 22 23 24 25 21 22 23 24 25 21 22 23 24 25
and you can use the same name j in every loop it's not problem because here. because it will reinitialized by the for loop every time. Hope this is clear an simple for you.
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
I have some code here:
for i in range(self.size):
print('{:6d}'.format(self.data[i], end=' '))
if (i + 1) % NUMBER_OF_COLUMNS == 0:
print()
Right now this prints as:
1
1
1
1
1
2
3
3
3
3
(whitespace)
3
3
3
etc.
It creates a new line when it hits 10 digits, but it doens't print the initial 10 in a row...
This is what I want-
1 1 1 1 1 1 1 2 2 3
3 3 3 3 3 4 4 4 4 5
However when it hits two digit numbers it gets messed up -
8 8 8 8 8 9 9 9 9 10
10 10 10 10 10 10 etc.
I want it to be right-aligned like this-
8 8 8 8 8 9
10 10 10 10 11 12 etc.
When I remove the format piece it will print the rows out, but there wont be the extra spacing in there of course!
You can align strings by "padding" values using a string's .rjust method. Using some dummy data:
NUMBER_OF_COLUMNS = 10
for i in range(100):
print("{}".format(i//2).rjust(3), end=' ')
#print("{:3}".format(i//2), end=' ') edit: this also works. Thanks AChampion
if (i + 1) % NUMBER_OF_COLUMNS == 0:
print()
#Output:
0 0 1 1 2 2 3 3 4 4
5 5 6 6 7 7 8 8 9 9
10 10 11 11 12 12 13 13 14 14
15 15 16 16 17 17 18 18 19 19
20 20 21 21 22 22 23 23 24 24
25 25 26 26 27 27 28 28 29 29
30 30 31 31 32 32 33 33 34 34
35 35 36 36 37 37 38 38 39 39
40 40 41 41 42 42 43 43 44 44
45 45 46 46 47 47 48 48 49 49
Another approach is to just chunk up the data into rows and print each row, e.g.:
def chunk(iterable, n):
return zip(*[iter(iterable)]*n)
for row in chunk(self.data, NUMBER_OF_COLUMNS):
print(' '.join(str(data).rjust(6) for data in row))
e.g:
In []:
for row in chunk(range(100), 10):
print(' '.join(str(data//2).rjust(3) for data in row))
Out[]:
0 0 1 1 2 2 3 3 4 4
5 5 6 6 7 7 8 8 9 9
10 10 11 11 12 12 13 13 14 14
15 15 16 16 17 17 18 18 19 19
20 20 21 21 22 22 23 23 24 24
25 25 26 26 27 27 28 28 29 29
30 30 31 31 32 32 33 33 34 34
35 35 36 36 37 37 38 38 39 39
40 40 41 41 42 42 43 43 44 44
45 45 46 46 47 47 48 48 49 49
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