Python_ler
Python_ler

Reputation: 63

How do I print a range from a incremented value to a decremented in python

I am new in python and I want to print a range, but when I run the program dosent print anything.

dec_value=int(input("ENTER THE DECREMENTED VALUE"))
for i in range(100,0,dec_value):
  print(i)

Can someone please tell me what I am doing wrong?

Upvotes: 2

Views: 2639

Answers (2)

pylang
pylang

Reputation: 44545

Do the following to normalize negative or positive inputs:

dec_value = int(input("ENTER THE DECREMENTED VALUE: "))
for i in range(10, 0, -abs(dec_value)):
    print(i)


>>> ENTER THE DECREMENTED VALUE: -5
10
5

>>> ENTER THE DECREMENTED VALUE: 5
10
5

Upvotes: 0

Carcigenicate
Carcigenicate

Reputation: 45806

dec_value (the step of the range) needs to be negative if the start number is larger than the end number, or it will return an empty range. If it returns an empty range, the loop won't run since there's nothing to iterate over.

If start < end, the step should be positive, else, it should be negative.

Upvotes: 5

Related Questions