英文:
Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> print(name + age) TypeError: can only concatenate str (not "int") to str
问题
我在编写上面的代码时遇到了一个错误,你能帮我吗?
```python
name = 'mahbod'
age = 12
print(name + age)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(name + age)
TypeError: can only concatenate str (not "int") to str
英文:
I was writing the above code when I encountered an error can you help me?
name = 'mahbod'
age = 12
print(name + age)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(name + age)
TypeError: can only concatenate str (not "int") to str
答案1
得分: 1
代码部分不需要翻译。以下是翻译好的内容:
It looks like you want to define two different variables: name
and age
.
In that case, you need to define each of them on a different line:
name = "mahbod"
age = 12
otherwise, here Python thinks that you are defining name
as "Mahbod" age.
In python, you can concatenate strings by separating them with a space, like this:
>>> string = "Hello " "world"
>>> print(string)
Hello world
so that's what Python is trying to do here, except that age
is not a string, but a variable that you defined as 12, hence the confusion
英文:
It looks like you want to define two different variables: name
and age
.
In that case, you need to define each of them on a different line:
name = "mahbod"
age = 12
otherwise, here Python thinks that you are defining name
as "Mahbod" age
.
In python, you can concatenate strings by separating them with a space, like this:
>>> string = "Hello " "world"
>>> print(string)
Hello world
so that's what Python is trying to do here, except that age
is not a string, but a variable that you defined as 12, hence the confusion
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论