Python Forum
Multiplication Table Homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplication Table Homework
#1
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? ")
Reply
#2
What's wrong with:

if (row * column) % 2 == 0:
    rowString += '#'
after line 25? And I think you want to unindent lines 29 and 30.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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? ")
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statements and multiplication elroberto 1 1,719 Jun-22-2022, 05:35 PM
Last Post: deanhystad
  Find the maximum multiplication ercv 3 2,164 Nov-30-2020, 11:55 AM
Last Post: DeaD_EyE
  creating a 3x3 multiplication table aditvaddi 1 3,624 Jun-18-2018, 06:05 AM
Last Post: volcano63
  Multiplication Table funnybone04 4 5,790 Apr-08-2018, 03:03 AM
Last Post: nilamo
  Nested Loop multiplication table SushiRolz 3 10,250 Feb-28-2018, 04:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020