How can I make a multiplication table by this way | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How can I make a multiplication table by this way

multiply = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] for i in multiply: b = i * 1 c = b + 1 c = b v = "2 " + "* " + str(i) + " = " + str(c) print(v)

11th Mar 2021, 7:36 PM
Muhammad Abdulmalik
Muhammad Abdulmalik - avatar
3 Antworten
+ 1
This will make a table. I'm using range(1, 13) instead of your multiply variable because it'll work in a similar way with less code. for i in range(1, 13): row = '' for j in range(1, 13): b = i * j b = str(b) # convert number to base 10 string. b = ' ' * (4 - len(b)) + b # add enough spaces to line up columns. row += b print(row) If you run locally, it should line up the columns. Sololearn's Code Playground Python codes won't line up because it uses a different font and doesn't recognize consecutive spaces differently than a single space.
11th Mar 2021, 7:51 PM
Josh Greig
Josh Greig - avatar
0
Muhammad Abdulmalik multiply = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] for i in multiply: c=2*i v = "2 " + "* " + str(i) + " = " + str(c) print(v)
11th Mar 2021, 7:43 PM
Abhay
Abhay - avatar
0
Firstly, you can just use for i in range(1,12), instead of the set, as it will do the exact same thing. Secondly, if you want to have it for all the numbers 1-12 times all numbers 1-12, then I would recommend using a nested loop with a for j in range(1,12) straight after the first for loop. Then replace b=i*1 with b=i*j, and that will go through everything. That leaves the two c variable definitions needless. Now we have the multiplied numbers (i & j) and the answer (b). Format that how you wish, and there we go. It is a bit inefficient to go through all cases with addition, so I would recommend this multiplication in nested loops. You were close! Just a bit of formatting needed and it will be brilliant!
11th Mar 2021, 7:49 PM
Kamil Hamid
Kamil Hamid - avatar