-1

When I try to make it print the items within the list it never runs the first if statement. Here is my code. number_of_numbers_multiplication is equal to number_of_numbers_multiplication so it shouldn't go to the 2nd statement right?

numbers = [3,2,1]
how_many_multiplication = 3
number_of_numbers_multiplication = int(how_many_multiplication)
first = True
for multiplication_printer in range (1,number_of_numbers_multiplication):
            if multiplication_printer == number_of_numbers_multiplication:
                    print(numbers[multiplication_printer])
            elif multiplication_printer > 1 and multiplication_printer != number_of_numbers_multiplication:
                    print(numbers[multiplication_printer],'multiplied by,')
            elif first == True:
                    print(numbers[0],'multiplied by,')
                    print(numbers[1],'multiplied by,')
                    first = False;

please help

Daemonique
  • 468
  • 2
  • 5
  • 15
  • Possible duplicate of [Why does range(start, end) not include end?](https://stackoverflow.com/questions/4504662/why-does-rangestart-end-not-include-end) – Thomas Kühn Jan 04 '18 at 08:10

1 Answers1

2

That is normal. The stop parameter number_of_numbers_multiplication of the range function is exclusive. If you want to reach it, use range(1,number_of_numbers_multiplication+1).

EDIT: Now, since you changed the value of number_of_numbers_multiplication and it is equal to 3. The solution would be to use range(1,number_of_numbers_multiplication) but use if multiplication_printer==number_of_numbers_multiplication-1

EDIT 2 The important things to keep in mind are:

1.multiplication_printer is used as an index, so it must stop at the length of the list-1 (3-1=2). 2. the stop parameter of range is exclusive, so if multiplication_printer must stop at 2, the parameter must be equal to 3.

  • but then it prints 3 multiplied by 2 multiplied by 1 multiplied by instead of 3 multiplied by 2 multiplied by 1 – Daemonique Jan 04 '18 at 08:13
  • if we go with number_of_numbers_multiplication, without the +1, we will have only one iteration. And it will execute the third statement. That would happen because multiplication printer would be equal to 1 and first=True. We won't have any other iterations because multiplication_printer goes from 1 to 2 exclusive, which means he never reaches 2. – glamorous_noob Jan 04 '18 at 08:28
  • You're welcome. I edited my answer to match the last edit of your question – glamorous_noob Jan 04 '18 at 08:33
  • Sorry for only accepting this now, I was new to stack overflow and must've not realised or forgot, thanks again. – Daemonique Nov 15 '19 at 15:18