1

For the question below I done what I can shown in , but don't really know where to go from there. I just started working with end values and am probably going to destroy this small code farther.

https://i.stack.imgur.com/QIuJt.png


# Inputs
range_start = int(input("Enter start value:"))
range_end = int(input("Enter end value:"))

# Calculations
for loop in range(range_start, range_end + 1):
answer = range_start + loop
print("{}|".format(loop), "{}".format(answer))

3 Answers3

1

You're going to need two loops. I'm going to make a 2 dimensional matrix (just a list of lists), where matrix[0][0] is 0+0.

mat = []
for i in range(0, end+1):
    mat.append([])
    for j in range(0, end+1):
        mat[i].append(i+j)

This isn't quite the output your assignment asks for, and I encourage you to go the last little bit on your own.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0

Maybe a little more compact:

matrix = [[i+j for i in range(start, end+1)] for j in range(start, end+1)]
Tammo Heeren
  • 1,966
  • 3
  • 15
  • 20
0

Here's the code I came up with. The main part of the script is that it uses str.format to allow for easier formatting with the numbers for the table. print_overall gets output strings from output. After printing the list of columns and the dashed line, print_overall prints row by row. I recommend checking out "6.1.3. Format String Syntax" in the docs for more info. about str.format.

def output(values = None, row_num = None):
    res = ""
    if not row_num:
        res += " " * 2
    else:
        res += row_num + "|"    
    for i in values:
        res += "{:5}".format(i) #{:5} allows for filler space when string length < 5
    return res

def print_overall(rnge):
    print(output(rnge))
    print(output(["-"*5]*len(rnge)))
    for row_num in rnge:
                #for every row number, make an list that maps values
                #where all the column numbers are added to the current row number
        lst = list(map(lambda col_num: row_num + col_num, rnge))
        print(output(lst, row_num = str(row_num)))


range_start = int(input("Enter start value:"))
range_end = int(input("Enter end value:"))

input_range = range(range_start, range_end+1) #+1 because range is exclusive at endpoint
print_overall(input_range)
DragonautX
  • 860
  • 1
  • 12
  • 22