Python Forum
inserting value into strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
inserting value into strings
#1
so hi I am a complete noob at python and only started learning the basics I tried to create many random things which seem to work but I have no idea how to make this one work, as I said before I am a total noob so don't scold me
>>> def Love():
B=int(len(input("please put your name:")))
G=int(len(input("please put the name og your love interest:")))
print("your compatibillity is" + B-G + "%...")
p.s i am using python v.3.6.2

User has been warned for this post. Reason: crappy title
Reply
#2
Inside the print function, you are using string concatenation. Since it's a string concatenation B - G produce integer which is not a string.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Aug-29-2017, 08:43 PM)wavic Wrote: Inside the print function, you are using string concatenation. Since it's a string concatenation B - G produce integer which is not a string.
im sorry but as i said before i am a noob so can you please make it easier to understand for me, i am sooo sorry for being a pain
Reply
#4
In the print() function you are telling Python to concatenate (add) two strings, 'B' and 'G', yet you have defined them as integers. 

>>> x = 'hello'
>>> y = 'world'
>>> x + y
'helloworld'
>>>
Since you are using Python 3.6.2, you should look into formatting. So your print() function would look like this:

print("your compatibillity is {} %".format(B + G))
Also, you have defined a function "Love", which you do not use. If B, G and "print" are meant to be part of the function, they need to be properly indented and the function called:

def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is {} %".format(B + G))

love()
Output:
C:\Python36\python.exe C:/Python/Games/scratch.py please put your name:joe please put the name og your love interest:sally your compatibility is 8 %
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
Alternatively you can use String formatters, %s
def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is %s" % str(B-G))
 
love()
Reply
#6
(Aug-29-2017, 09:11 PM)sparkz_alot Wrote: In the print() function you are telling Python to concatenate (add) two strings, 'B' and 'G', yet you have defined them as integers. 

>>> x = 'hello'
>>> y = 'world'
>>> x + y
'helloworld'
>>>
Since you are using Python 3.6.2, you should look into formatting. So your print() function would look like this:

print("your compatibillity is {} %".format(B + G))
Also, you have defined a function "Love", which you do not use. If B, G and "print" are meant to be part of the function, they need to be properly indented and the function called:

def love():
    B = int(len(input("please put your name: ")))
    G = int(len(input("please put the name of your love interest: ")))
    print("your compatibility is {} %".format(B + G))

love()
Output:
C:\Python36\python.exe C:/Python/Games/scratch.py please put your name:joe please put the name og your love interest:sally your compatibility is 8 %

thx dude ur awesome!! i understand now and i think i will mess around with this... Thx again! Dance Dance
Reply
#7
What I have said is that you adding the result of B - G which is integer to the end of the string using string concatenation - the '+' sign. But in order to do that you have to turn the result to an string first: print("your compatibillity is" + str(B-G) + "%..."). Or just to use string formatting that was proposed already.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
i think i finally found out how to do it XP
def Love():
def Love():
	b=int(len(input("put in your name please: ")))
	g=int(len(input("please put in the name of your crush: ")))
	l=int(((((b*4)**4)-b**2)-g**5)+((((g*3)**2)-g**2)-b**4))
	b=int(l/9000)
	print("--------------------------------------------------------------------------")
	print("your love compatibillity is " + str(b) + "%; Disclaimer: this is not accurate at all")
yay
Reply
#9
Too many
int
, unnecessary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 779 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,782 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,534 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,246 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020