adam
adam

Reputation: 173

Missing output, using range() in Python

I am not getting the user output for an end-of-chapter problem in the Python book I'm reading.

Question is:

Write a program that counts for the user. Let the user enter the starting number, the ending number, and the amount by which to count.

This is what i came up with:

start = int(input("Enter the number to start off with:"))
end = int(input("Enter the number to end.:"))
count = int(input("Enter the number to count by:"))

for i in range(start, end, count):
    print(i)

after this input nothing happens except this:

Enter the number to start off with:10
Enter the number to end.:10
Enter the number to count by:10

Upvotes: 1

Views: 609

Answers (2)

James K
James K

Reputation: 3752

Remember the range(start, stop, count) begins at start, but finishes before stop.

So range(10,10,10) will try to produce a list that starts at 10, and stops before 10. In other words there is nothing in the list, and the print statement is never reached.

Try again with other numbers: start at 5, stop before 12, and count by 2 should give a more satisfactory result.

Upvotes: 1

Phillip Cloud
Phillip Cloud

Reputation: 25652

range(10, 10, 10) will generate an empty list because range constructs a list from start to stop EXCLUSIVE, so you're asking Python to construct a list starting from 10 going up to 10 but not including 10. There are exactly 0 integers in between 10 and 10 so Python returns an empty list.

In [15]: range(10, 10, 10)
Out[15]: []

There's nothing to iterate over so nothing will be printed in the loop.

Upvotes: 9

Related Questions