0

Need to have the range() value to be based on whatever a user inputs as starting and ending factors. It also need headers for rows and columns. Please use simple syntax I'm new to programming!

# This program creates a multiplication table with nested loops and limits the
# factors based on user input values.
#
# References:
# https://www.youtube.com/watch?v=0rWHH5JfKdo
# https://www.youtube.com/watch?v=Dpkfh-hX4-M
# https://stackoverflow.com/questions/27312273/python-meaning-of-end-in-the-
# statement-print-t-end/27312325



def nested_loop(start, end):
    for i in range(start, end + 1):
        for j in range(start, end + 1):
            print(i * j, end='\t')
        print('')


def get_value(label):
    print("Enter " + label + " value:")
    value = int(input())
    return value


def main():
    start = get_value("starting")
    end = get_value("ending")
    nested_loop(start, end)


main()
martineau
  • 119,623
  • 25
  • 170
  • 301
userh16xx0
  • 37
  • 8
  • This is completely off-topic: Stack Overflow is not a free code writing service, nor is it meant to provide tutoring or guides. Please see [ask], [help/on-topic]. – AMC Mar 20 '20 at 02:13
  • You're right! Thats why I wrote most of this code and I only wanted to know how to add headers (which was two lines) :) – userh16xx0 Mar 20 '20 at 02:17

2 Answers2

1

Your code already do most of the job, it prints a 2d matrix of the multiplication table in the period specified by the user, you only missing the headers part which could be added like this

def nested_loop(start, end):
    # printing the top header
    print('', end='\t')
    for i in range(start, end + 1):
        print(i, end='\t')
    print('')

    for i in range(start, end + 1):
        print(i, end='\t') # printing the running column alongside the matrix
        for j in range(start, end + 1):
            print(i * j, end='\t')
        print('')


def get_value(label):
    print("Enter " + label + " value:")
    value = int(input())
    return value


def main():
    start = get_value("starting")
    end = get_value("ending")
    nested_loop(start, end)


main()

Using input like 3, 10

the program prints out:

    3   4   5   6   7   8   9   10  
3   9   12  15  18  21  24  27  30  
4   12  16  20  24  28  32  36  40  
5   15  20  25  30  35  40  45  50  
6   18  24  30  36  42  48  54  60  
7   21  28  35  42  49  56  63  70  
8   24  32  40  48  56  64  72  80  
9   27  36  45  54  63  72  81  90  
10  30  40  50  60  70  80  90  100 
kareem_emad
  • 1,123
  • 1
  • 7
  • 12
0

Agree with the accepted answer but just have to show how python lovers tend to complicate things... something like... this:

def nasty_loop(start, end):
    print('\t'+'\t'.join(str(x) for x in range(start,end+1)))
    for y in range(start, end+1):
        print("%d\t" % y + '\t'.join(str(x*y) for x in range(start,end+1)))
Boris Lipschitz
  • 1,514
  • 8
  • 12
  • Welp I'm just beginning to learn python and this looks crazy to me – userh16xx0 Mar 20 '20 at 00:13
  • @userh16xx0 actually it isn't crazy, but way more complicated to understand at the first sight. Is it a good thing to code like this? Probably not :-D – Boris Lipschitz Mar 20 '20 at 00:14
  • I hope I can reach that level of coding soon – userh16xx0 Mar 20 '20 at 00:18
  • @userh16xx0 sure you can, it only looks a bit complex. And over-complicating simple things isn't really something to brag about, so the real question, while you definitely can, should you? I've posted it just to show how python fans tend to over-complicate simple stuff. This code isn't better than the accepted answer in any way or form. – Boris Lipschitz Mar 20 '20 at 00:21