I have been working on making a multiplication table using a function, a for loop and string formatting. I tried a small-scale test table going up to twenty, which printed fine. then, I went all the way up to 100, and I got this error: "ValueError: cannot switch from manual field specification to automatic field numbering" and I don't know why. I do have a very large code, and I may have made some spelling mistakes. I realize that there is probably an easier way of doing it, but I am just unaware of it. I don't know advanced Python methods, sadly. Here is the up-to-twenty code which I wrote:
def createtable():
layout = "{0:>5} {1:>5} {2:>5} {3:>5} {4:>5} {5:>5} {6:>5} {7:>5} {8:>5} {9:>5} {10:>5} {11:>5} {12:>5} {13:>5} {14:>5} {15:>5} {16:>5} {17:>5} {18:>5} {19:>5} {20:>5}"
print(layout.format(" ","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"))
print(" |------------------------------------------------------------------------------------------------------------------------")
for i in range(1,21):
print(layout.format(str(i)+"|",1*i,2*i,3*i,4*i,5*i,6*i,7*i,8*i,9*i,10*i,11*i,12*i,13*i,14*i,15*i,16*i,17*i,18*i,19*i,20*i))
and here is the code that I couldn't get to work:
def createtable2():
layout = "{0:>5} {1:>5} {2:>5} {3:>5} {4:>5} {5:>5} {6:>5} {7:>5} {8:>5} {9:>5} {10:>5} {11:>5} {12:>5} {13:>5} {14:>5} {15:>5} {16:>5} {17:>5} {18:>5} {19:>5} {20:>5} {21:>5} {22:>5} {23:>5} {24:>5} {25:>5} {26:>5} {27:>5} {28:>5} {29:>5} {30:>5} {31:>5} {32:>5} {34:>5} {35:>5} {36:>5} {37:>5} {38:>5} {39:>5} {40:>5} {41:>5} {42:>5} {:43>5} {44:>5} {45:>5} {46:>5} {47:>5} {48:>5} {49:>5} {50:>5} {51:>5} {52:>5} {53:>5} {54:>5} {55:>5} {56:>5} {57:>5} {58:>5} {59:>5} {60:>5} {61:>5} {62:>5} {63:>5} {64:>5} {65:>5} {66:>5} {67:>5} {68:>5} {69:>5} {70:>5} {71:>5} {72:>5} {73:>5} {74:>5} {75:>5} {76:>5} {77:>5} {78:>5} {79:>5} {80:>5} {81:>5} {82:>5} {83:>5} {84:>5} {85:>5} {86:>5} {87:>5} {89:>5} {90:>5} {91:>5} {92:>5} {93:>5} {94:>5} {95:>5} {96:>5} {97:>5} {98:>5} {99:>5} {100:>5}"
print(layout.format(" ","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","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100"))
print(" |------------------------------------------------------------------------------------------------------------------------")
for i in range(1,21):
print(layout.format(str(i)+"|",1*i,2*i,3*i,4*i,5*i,6*i,7*i,8*i,9*i,10*i,11*i,12*i,13*i,14*i,15*i,16*i,17*i,18*i,19*i,20*i))
I don't know what is wrong. Could someone please help me?
You made 3 typos in layout
:
{33:>5}
{88:>5}
{:43>5}
instead of {43:>5}
It is the final typo that gave rise to the error message you see.
This simply goes to show that manually typing in such a long list is a bad idea as you have a high risk of doing something wrong.
Additionally, the length of layout
must match the number of arguments you pass to layout.format
. Because of this, you would have gotten another error in the for loop, as the number of arguments in the final layout.format
is 21, whereas the layout
variable has length 101 (without typos).
I would recommend a generalized approach to such a problem instead, removing the risk of passing a wrong number of arguments, and creating typos. See example below:
def create_table(max_row, max_col):
layout = ' '.join([f'{{{x}:>5}}' for x in range(max_col + 1)])
fmt = [str(x) for x in range(1, max_col + 1)]
print(layout.format(" ", *fmt))
brk = '------' * max_col
print(f" |{brk}")
for i in range(1, max_row + 1):
cols = [x * i for x in range(1, max_col + 1)]
print(layout.format(str(i) + "|", *cols))
>>> create_table(max_row=24, max_col=15)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|------------------------------------------------------------------------------------------
1| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2| 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
3| 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
4| 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5| 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
6| 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
7| 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
8| 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
9| 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
10| 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
11| 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
12| 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
13| 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
14| 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
15| 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
16| 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240
17| 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255
18| 18 36 54 72 90 108 126 144 162 180 198 216 234 252 270
19| 19 38 57 76 95 114 133 152 171 190 209 228 247 266 285
20| 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300
21| 21 42 63 84 105 126 147 168 189 210 231 252 273 294 315
22| 22 44 66 88 110 132 154 176 198 220 242 264 286 308 330
23| 23 46 69 92 115 138 161 184 207 230 253 276 299 322 345
24| 24 48 72 96 120 144 168 192 216 240 264 288 312 336 360