0

I'm using Pycharm and I'm coding something that asks you questions. This is the code: https://repl.it/@NateyBoi/Bitch-Lasagna

the addition doesn't seem to work.

So you type your name and then type in your favorite color. then you put in the numbers of letters there are in your name as your x, and then do the same for your favorite color, which would be your y. Then It adds x(how much letters are in your name) to y(how much letters are in your favorite color). then it adds x to y and you get an answer. For me, I would Put 7 as my x and then 5 as my y and the answer becomes 75. 7 plus 5 is 12 not 75. It looks like it puts those too together and then it out puts it. I tried Rewriting the code and then I looked it up on google and all the things I saw just showed me what would happen if it was exucuted correctly.

print("Hello there.")
name = input("What is your name? ")
color = input("What is your favorite color? ")
print("So " + name + " Likes the color " + color)
x = input("How much letters are in your name")
print("So " + x + " Letters in your name?") 
y = input("Ok now how much letters in your favorite color?")
print("So " + y + " Letters in your favorite color?")
print(x + y) 

I expect It to add x to y and then It would out put to what it equals(an example would be 7 letter name for x, 5 letter color for y and then get 12, put it outputs it as 75 and not 12)

2 Answers2

0

You can try:

x = int(input("How much letters are in your name"))
y = int(input("Ok now how much letters in your favorite color?"))

That is because the x and y in your code are strings, you need to transfer them into integer so python knows you are adding two numbers together not joining two strings.

0

As the comments said, it is because you are concatenating two strings rather than adding two numbers. To fix the code replace:

print(x+y)

With:

print(int(x)+int(y))

Or your code block fixed:

print("Hello there.")
name = input("What is your name? ")
color = input("What is your favorite color? ")
print("So " + name + " Likes the color " + color)
x = input("How much letters are in your name")
print("So " + x + " Letters in your name?") 
y = input("Ok now how much letters in your favorite color?")
print("So " + y + " Letters in your favorite color?")
print(int(x) + int(y)) 
Will
  • 1,059
  • 2
  • 10
  • 22