1

I am writing a program in which it needs to determine the range of the number as if I put 15 in user_input 'a' so it should print range from 'a' to 'b' but unfortunately i'm unable to work it out can anyone please help me with this. This is my code:

a = int(raw_input("How many did you say you're going to count down? "))
b = int(raw_input("When are you actually going to stop? "))
i = 0
for i in range(a, b):
    i = i + 1
    print i 

and I want it to work like this:

How many did you say you're going to count down? 15
When are you actually going to stop? 8
15
14
13
12
11
10
9
8

OR

How many did you say you're going to count down? 6
When are you actually going to stop? 4
6
5
4

1 Answer 1

5

The loop can be:

a = int(raw_input("How many did you say you're going to count down? "))
b = int(raw_input("When are you actually going to stop? "))

for i in range(a, b-1, -1):
    print i 

(assuming the count decreases).

What you must know is:

  1. The for loop does the decrement for you (no need for i = i-1 like in C).
  2. range(a, b-1, -1) is a list (in Python 2) that goes from a to b-1 (not included), by steps of -1. You can try to do, in a Python shell, print range(10, 5, -1), for instance. You can also check the output of range(5, 11, 2), to understand better what range() does.
Sign up to request clarification or add additional context in comments.

1 Comment

@rocker789: Now it does. Your loop was counting upwards (twice).

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.