Ask coding questions

← Back to all posts
Trouble with numbers and text in the same line (Python 3)
KrystanKlukos (1)

I've been using a code for awhile trying to get text and variables in the same line to print a sentence that depends mainly changing variables based on int(inuput) it doesn't work

Commentshotnewtop
Geocube101 (606)

You could convert the integers/floats into strings via the str() method.

a = int(input("Enter Some Number"))
b = a + 1
print("New Number = " + str(b))

BlueLightning42 (1)

Like this?
a = int(input("Enter a large number"))
b = a + 1
print("Okay thats pretty large but {} is larger".format(b))

KrystanKlukos (1)

@BlueLightning42
a = int(input())
b = (a - 1 )
c = (a + 1)
d = ("The next number for the number ")
e = ("is ")
f = ("The previous number for the number ")
print(d + a + e + b )
I did it in this format, but I don't exactly know how to fix it

BlueLightning42 (1)

@KrystanKlukos just so you know
if your repl is python 3 (not python 2) and cause print is a function in python 3 you could also pass multiple parameters to it/ write something like this
a = int(input())
b = a - 1
c = a + 1
d = "The next number for the number "
e = "is "
f = "The previous number for the number "
print(d, a, e , b, f , c)

using format is the prefered way of going about it

leon332157 (217)

Can u provide an example or a repl link please?

KrystanKlukos (1)

@leon332157 a = int(input())
b = (a - 1 )
c = (a + 1)
d = ("The next number for the number ")
e = ("is ")
f = ("The previous number for the number ")
print(d + a + e + b )

leon332157 (217)

@KrystanKlukos Ok, so first of all, (a-1) is a tuple which contains integer a-1, assigned to b, same for c, and d and e is a tuple with string in it, so if you call print function with these data type, it will not work.

leon332157 (217)

@KrystanKlukos a = int(input())
b = a - 1
c = a + 1
d = "The next number for the number "
e = "is "
f = "The previous number for the number "
print('{}{}{}{}'.format(d, a ,e , b ))

leon332157 (217)

@KrystanKlukos And this is also a Python basics, I would suggest you to review data types, operations, input and print functions. :)

BlueLightning42 (1)

@leon332157 beat me by a few seconds but kudos

leon332157 (217)

@BlueLightning42 What is that for? We always help ppl ;)

KrystanKlukos (1)

@BlueLightning42 Both of you are appreciated haha