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
layoutmust match the number of arguments you pass tolayout.format. Because of this, you would have gotten another error in the for loop, as the number of arguments in the finallayout.formatis 21, whereas thelayoutvariable 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: