1

I searched for other answers but couldn't get it right. It'd be awesome if someone could help me :) I keep getting this error: TypeError: can only concatenate str (not "NoneType") to str | on this code. I was trying to write a code that makes automatic e-mail based on the name of the person plus the ammount of characters in their name.

name = input("whats your name?")

number_of_letters = len(name+ "gmail.com")

print(len(name))

print((name) + (len(name)) + ("@gmail.com"))
Paul Randleman
  • 104
  • 1
  • 8
  • Which line? I suspect this isn't your actual code, and you are actually doing something like `print(...) + len(...)`. – chepner Sep 29 '22 at 23:45
  • 1
    As for the error in the title, you can't add `name + len(name)`, either; you'd have to convert the `int` that `len` returns back to a `str` first: `name + str(len(name))`. – chepner Sep 29 '22 at 23:46
  • 1
    Does this answer your question? [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Paul Randleman Sep 29 '22 at 23:47

2 Answers2

0

You can't add int and str,

You just need to change this line,

print((name) + str(len(name)) + ("@gmail.com"))
#              ^ edited here
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

len(name) returns an int value, so when you do name + len(name), Python doesn't know if it should be adding the values or concatenating them. You can use str(len(name)) to make it clear that you want len(name) to be treated as a string. Also, the parentheses around each piece aren't necessary (although they won't cause any problems either).

print(name + str(len(name)) + "@gmail.com")
dptaylor
  • 46
  • 3