Ask coding questions

← Back to all posts
HELP ME!!!!!!!!!!!!!!!
FARHEENPALAGIRI (11)

So I was trying to write this code for this situation:

Write a program that reads an integer number and prints its previous and next numbers

To solve this I wrote a code that had two different variables, one for the previous number and one for the next number. It looked like this:

a = int(input())
previous = a - 1
next = a + 1

So I'm trying to print the entire statement "The number after 180(ex) is 179" in one line. To do this, I typed:

print ("the number after " + a + "is " + previous)

Whenever I try to test this, the server always states that I need to change 'str' to 'int'. I do not know what to do for this issue. I'm new. Help me please.

Answered by CoolqB (150) [earned 5 cycles]
View Answer
Commentshotnewtop
CoolqB (150)

The problem here is you're trying to add a string type to an integer. You cannot add a string (a bunch of letters) and a number, because you just can't. To fix this, using the str() function (short for string), tell python to turn the numbers into a string. Try this: print ("the number after " + str(a) + "is " + str(previous)). This is a simplified version of what's really going on here, so I recommend you look up about datatypes, it'll help you a lot!

zog1 (15)

@CoolqB ^^^^ Really said it all.

leon332157 (217)

Because + in Python is for adding strings. U can either convert the int into a string, or use ',' instead of '+' .

AzureScripts (172)

I have done what you tried to do in one of my repls: https://repl.it/@AzureScripts/Integer-Help

It is working and it has the same function as you tried to make!
Take a look and tell me if this works