0

The program calculates the multiplication table from 0 to 12 for the number the user has entered. I am trying to display the contents of the array with the results in it by for looping through it. However, it only displays the tables from 0 to 11, but not the 12th one, even if the 12th data is there.

Here is what I came up with:

def multiplicationTable():
 nb = int(input("Please enter a number between 1 and 12 : \n"))

 table = array('i')
 for i in range(0,12):
     table.append(i * nb)

 for i in range(len(table)):
     print(str(nb) + "x" + str(i) + " = " + str(table[i]))

The output looks like this:

4x0 = 0
4x1 = 4
4x2 = 8
4x3 = 12
4x4 = 16
4x5 = 20
4x6 = 24
4x7 = 28
4x8 = 32
4x9 = 36
4x10 = 40
4x11 = 44

What could be causing that ? Coming from VB and C# so I may be mistaking the i as an index, while it is a value from the array, but I really don't see how I could be fixing this. Thank you !

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jsgarden
  • 1
  • 3
  • `range(0,12)` goes from `0` to `11`. So that is why the `i` value never reaches 12. If you want for loop to go from `0` to `12` just make the for loop use `range(0, 13)` – Karl Oct 18 '18 at 02:23
  • You should modify your range to `range(1,13)` – jar Oct 18 '18 at 02:25
  • Why do you have `array` in your code? I don't think python has that unless its self declared function. – jar Oct 18 '18 at 02:27

1 Answers1

1

The range() function is non-inclusive of the upper bound. So when you're saying range(0,12), you're only going to get [0,11]. If you want [0,12], you have to do range(0,13). Note that range(0,13) is equivalent to range(13) because the default lower-bound is 0.
See the documentation.

Mark
  • 125
  • 6