1

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

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.