Skip to content
← Back to Community
How to solve this problem?
Profile icon
MdAlAminAlAmin

How to solve this problem? https://snakify.org/en/lessons/print_input_numbers/problems/previous_and_next/

Previous and next

Statement
Write a program that reads an integer number and prints its previous and next numbers. See the examples below for the exact format your answers should take. There shouldn't be a space before the period.
Remember that you can convert the numbers to strings using the function str.

Tests
Input Correct answer
179 The next number for the number 179 is 180.
The previous number for the number 179 is 178.

0 The next number for the number 0 is 1.
The previous number for the number 0 is -1.

100 The next number for the number 100 is 101.
The previous number for the number 100 is 99.

Answered by vedprad1 [earned 5 cycles]
View Answer
Voters
Profile icon
EOlatunji
Profile icon
olvinkabela
Profile icon
MdAlAminAlAmin
Comments
hotnewtop
Profile icon
vedprad1

Assuming that the code is in python, this would be your answer:

The code is simple. You just have to take a variable and return the number 1 above it and the number 1 below it. The final code would look like this:

num = int(input()) print('The next number for the number ' + str(num) + ' is ' + str(num + 1) + '.') print('The previous number for the number ' + str(num) + ' is ' + str(num - 1) + '.')

If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

Profile icon
MdAlAminAlAmin

@vedprad1 I gave the same code but,
Traceback (most recent call last):
File "main.py", line 2, in
print('The next number for the number ' + num + ' is ' + (num + 1) + '.')
TypeError: can only concatenate str (not "int") to str

Profile icon
vedprad1

@MdAlAminAlAmin : You have to wrap all of the number variables in an str function, like this:

str(num)
Profile icon
teaganturner

Found your forum interesting to read. I can't wait to see your more forums soon. Good Luck for the upcoming update.This forum is really very interesting and effective. I learn about custom dissertation writing sites after reading your forum. When I was looking for more useful blogs I saw a blog that attract me a lot because It explains how to cite dissertation apa or dissertation using APA style. By simply following the guidelines in this blog, you also can arrive at your APA citation or referencing in no time.