Reading ints in input in a Python program

Question:

I have to take input from the user then the operator has to print what number is before the input and after the input like:

input= 3
has to print 2 and 4

I used range, did I do it wrong? I am just a beginner in Python.

number=int(input)

for num in range(number ,+ 1, -1):
    print(num)
Asked By: MelikeOzgirgin

||

Answers:

You first need to use input() to let the user register a number.

Then, simply print the number with number - 1 and number + 1.

number = int(input("What is your number? "))
print(f"{number - 1} {number + 1}")

Outputs to:

What is your number? 3
2 4
Answered By: Antoine Delia

you don’t need range do this task

num = int(input())

print(num-1, num+1)

Other answers which say that you don’t require a loop are perfectly fine, however I want to add a little suggestion in case you need to print more than just the number before and after. Maybe (as your first implementation suggests) you want to print the 5 numbers before and after. In this case you can do:

R = 5 #range
N = int(input("Enter your number. "))

numbers = [ i for i in range(N-R,N+R+1) if i != N]

print(numbers)

Answered By: Fra93
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.