-3

I need to write an algorithm that can print a range of 2 given numbers from the user and exclude them.

This is the code that I have so far yet this will exclude only the second number.

x = int(input("Input the first number for the range: "))
n = int(input("Input the second number for the range: "))

if (x and n != 0):
    for num in range(x, n):
        print(num)

How can I make it exclude the first number, too?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • `range` includes the start and excludes the stop. Use `range(x+1, n)`. Also while `if (x and n != 0):` will check that both numbers are not null, I suspect you misunderstood how this works. `if x and n:` would work. – mozway Jul 24 '22 at 18:45
  • 2
    Look at the documentation for [`range`](https://docs.python.org/3/library/functions.html#func-range). If you want to exclude the beginning, start from the next larger number - `x + 1`. – MattDMo Jul 24 '22 at 18:45
  • You might also be interested in https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value. `if (x and n != 0)` isn't doing what you think it's doing. – MattDMo Jul 24 '22 at 18:48
  • Did you notice what happened when the input for the first number is zero? – DarkKnight Jul 24 '22 at 19:02

1 Answers1

0

range can be invoked in 1 of two ways:

range(stop)
range(start, stop, [step])

You need to be aware that the stop value is excluded but start is included.

Therefore, for your use-case it's as simple as:

x = int(input("Input the first number for the range: "))
n = int(input("Input the second number for the range: "))


for num in range(x+1, n):
    print(num)

Note:

Input validation not included

DarkKnight
  • 19,739
  • 3
  • 6
  • 22