Still pretty new to programming and Python but I am picking it up and enjoy learning. This is part of a homework lab which just asks to create a simple multiplication table that ranges from 2x2 to 10x10. No problem but it also wants me to follow every even number in the table up with a "#". I have tried countless methods but everything I try either throws the table into chaos or pulls up an exception. A little bit of guidance as to what I'm missing in my code would be a huge help. Here's what I have on my own:
print("\t\t\t Multiplication Table")
print("===================================================================================\n")
# top row
choice= "y"
while choice.lower() =="y":
num=int(input("What size table would you like? Enter integer from 2-10: "))
if num < 2 and num > 10:
print("Please enter a integer in the range of 2-10. ")
else:
print()
string=''
topRowString = ''
for column in range(1,num+1):
topRowString += '\t' + str(column)
print(topRowString)
print("===================================================================================")
string += topRowString + '\n'
# subsequent rows
for row in range(1,num+1):
rowString = str(row)
for column in range(1,num+1):
rowString += '\t' + str(row*column)
print(rowString)
string += rowString + '\n'
choice = input("Would you like another table? ")
What's wrong with:
if (row * column) % 2 == 0:
rowString += '#'
after line 25? And I think you want to unindent lines 29 and 30.
(Nov-22-2018, 10:43 PM)ichabod801 Wrote: [ -> ]What's wrong with:
if (row * column) % 2 == 0:
rowString += '#'
after line 25? And I think you want to unindent lines 29 and 30.
That did the trick. I was right there but I just didn't have the code quite right. I was trying along the lines of:
if (row*column) % 2 == 0:
print(rowString + '#')
Glad to know that I was close.
One other question. Any obvious reason why it seems to be ignoring line 7's if statement? I want it to pull up an error if the user inputs anything under 2 or over 10, but when I test the program it allows any input.
(Nov-23-2018, 03:48 PM)mcnhscc39 Wrote: [ -> ]One other question. Any obvious reason why it seems to be ignoring line 7's if statement? I want it to pull up an error if the user inputs anything under 2 or over 10, but when I test the program it allows any input.
That should be 'or', not 'and'. A number cannot be simultaneously less than 2 and more than 10.
(Nov-23-2018, 03:54 PM)ichabod801 Wrote: [ -> ] (Nov-23-2018, 03:48 PM)mcnhscc39 Wrote: [ -> ]One other question. Any obvious reason why it seems to be ignoring line 7's if statement? I want it to pull up an error if the user inputs anything under 2 or over 10, but when I test the program it allows any input.
That should be 'or', not 'and'. A number cannot be simultaneously less than 2 and more than 10.
That makes sense. Thank you for your help. I turned it in although I did not completely format it the way I wanted. I wanted to get it to where the rows were aligned on the right side, but I just could not seem to get anything to work. I feel as if I was putting the code in the wrong if statement. How would you go about formatting it to the right side, for peace of mind sake. This is what I ended up with.
print("\t\t\t Multiplication Table")
print("===================================================================================\n")
# top row
choice= "y"
while choice.lower() =="y":
num=int(input("What size table would you like? Enter integer from 2-10: "))
if num < 2 or num > 10:
print("Please enter a integer in the range of 2-10. ")
else:
print()
string=''
topRowString = ''
for column in range(1,num+1):
topRowString += '\t' + str(column)
print(topRowString)
print("===================================================================================")
string += topRowString + '\n'
# subsequent rows
for row in range(1,num+1):
rowString = str(row)
for column in range(1,num+1):
rowString += '\t' + str(row*column)
if (row*column) % 2 == 0:
rowString += ' #'
print(rowString)
string += rowString + '\n'
choice = input("Would you like another table? ")
Keeping it simple, this is how I would change it:
widths = [len(str(column * num)) for column in range(num + 1)]
# subsequent rows
for row in range(1,num+1):
rowString = str(row)
for column in range(1,num+1):
num_text = str(row * column)
spaces = ' ' * (widths[column] - len(num_text))
rowString += '\t' + spaces + num_text
if (row*column) % 2 == 0:
rowString += ' #'
Calculate the maximum width of each column ahead of time (widths variable). Then add a number of spaces equal to the width of the current number subtracted from the maximum width for that colum (spaces variable, using the num_text variable).
Moving on to things you may not be familiar with, check out the format method of strings. This allows you to specify the width of a string, and whether to center it (for the column headers) or right/left align it. You can see the full format method syntax
here. That's what I usually use for making tables.
(Nov-25-2018, 04:41 AM)ichabod801 Wrote: [ -> ]Keeping it simple, this is how I would change it:
widths = [len(str(column * num)) for column in range(num + 1)]
# subsequent rows
for row in range(1,num+1):
rowString = str(row)
for column in range(1,num+1):
num_text = str(row * column)
spaces = ' ' * (widths[column] - len(num_text))
rowString += '\t' + spaces + num_text
if (row*column) % 2 == 0:
rowString += ' #'
Calculate the maximum width of each column ahead of time (widths variable). Then add a number of spaces equal to the width of the current number subtracted from the maximum width for that colum (spaces variable, using the num_text variable).
Moving on to things you may not be familiar with, check out the format method of strings. This allows you to specify the width of a string, and whether to center it (for the column headers) or right/left align it. You can see the full format method syntax here. That's what I usually use for making tables.
Thank you. I'll have to study up on this and practice because I am quite unfamiliar with it, as would probably be expected for a beginner. Again, thank you for all your help.