Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Where does this seemingly irrelevant number come from when using the range() module in Python?

I am asked to create two input variables (low and high) and output the odd numbers between both. I decided to use range() module and gave 3 to low, and 7 to high, as their values. The program is expected to return only 5, but it returned 5 and 3.
I drop a screenshot of the terminal below.
click here to see

low = int(input("enter the first number: "))
high = int(input("enter the second number: "))

def program(low, high):
    numbers = range(low, high, 2)
        
    for n in numbers:
        print(n)
        

program(low, high)

So how could I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The range(low, high, 2) is such that

low is included, high is not included, skip=2

Since you gave low=3 and high=7, the numbers will be 3 and skip 2 to get 5 (and 7 is not included)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading