-1
def printPreviousAndNext():
  take = input()
  print("The next number for the number" + take + "is" + int(take+1))
  print("The previous number for the number" + take + "is" + int(take-1))
printPreviousAndNext()

As you can see I want to give an input and want to print its previous and next numbers for example:

my input: 200

What I want the result to be is:

The next number for the number 200 is 201
The previous number for the number 200 is 199

But I am getting an error which is as follows:

Traceback (most recent call last):
  File "python", line 5, in <module>
  File "python", line 3, in printPreviousAndNext
TypeError: can only concatenate str (not "int") to str

Where is my mistake?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Cryptic Coder
  • 13
  • 1
  • 6
  • The error message seems clear, `"a" + 1` isn't something Python lets you do. Have you researched it? Looked into the various string formatting methods? – jonrsharpe Sep 07 '20 at 12:37

1 Answers1

1

The error message really says it all, you are trying to concat a string and a number, what you really want is to concatenate two strings.

def printPreviousAndNext():
  take = int(input())
  print(f"The next number for the number {take} is {take+1}")
  print(f"The previous number for the number {take} is {take-1}")
printPreviousAndNext()

Formatted with f-string even takes care of the convertions. (@Matthias)

Josef
  • 2,869
  • 2
  • 22
  • 23