String concatenate TypeError: can only concatenate str (not "int") to str"; [英] String concatenate TypeError: can only concatenate str (not "int") to str"
本文介绍了String concatenate TypeError: can only concatenate str (not "int") to str";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习教程,但出现错误.
I am following a tutorial and I am getting an error.
我的代码应该是这样的:
My code should be this:
salaries = {'John':'20','Sally':'30','Sammy':'15'}
print(salaries['John'])
salaries['John'] = salaries['John'] + 30
print(salaries['John'])
我收到这样的错误
回溯(最近一次调用最后一次):文件print.py",第 9 行,在薪水['约翰'] = 薪水['约翰'] + 30 类型错误:只能将str(不是int")连接到str
Traceback (most recent call last): File "print.py", line 9, in salaries['John'] = salaries['John'] + 30 TypeError: can only concatenate str (not "int") to str
你能帮我解决这个问题吗?
Can you help me with this?
推荐答案
这应该可以解决它:
salaries['John'] = str(int(salaries['John']) + 30)
您需要将 John 的工资转换为 int add 30,然后将其转换回字符串.
You need to convert the salaries of John to an int add 30 and then convert it back to a string.
这会将 salaries['John']
从 20 更改为 50
This will change salaries['John']
from 20 to 50
这篇关于String concatenate TypeError: can only concatenate str (not "int") to str";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文