Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:只能将str(而不是“int”连接到str123_Python - Fatal编程技术网

Python TypeError:只能将str(而不是“int”连接到str123

Python TypeError:只能将str(而不是“int”连接到str123,python,Python,运行以下代码后,我遇到一个错误 代码如下: a=input("enter the string value") b=int(input("enter the number")) c=a+b print(c) 结果如下: enter the string value xyz enter the number 12 Traceback (most recent call last): File "e:/python learning/error1.py", line 3, in <mod

运行以下代码后,我遇到一个错误

代码如下:

a=input("enter the string value")
b=int(input("enter the number"))
c=a+b
print(c)
结果如下:

enter the string value xyz
enter the number 12
Traceback (most recent call last):
  File "e:/python learning/error1.py", line 3, in <module>      
    c=a+b
TypeError: can only concatenate str (not "int") to str
输入字符串值xyz
输入数字12
回溯(最近一次呼叫最后一次):
文件“e:/python learning/error1.py”,第3行,在
c=a+b
TypeError:只能将str(而不是“int”)连接到str
使用以下方法:

a=输入(“输入字符串值”)
b=int(输入(“输入数字”))
c=a+str(b)
印刷品(c)
输出

enter the string valuexyz
enter the number12
xyz12

如果您的最终目标是连接,则不需要将输入转换为int,只需将其用作输入:

a=input("enter the string value")
b=input("enter the number")
c=a+b
print(c)

+
与字符串一起使用时,只能将其与其他字符串连接起来。但是,您尝试将其与整数连接

c=a+b
更改为
c=a+str(b)


str(b)
将整数的
b
转换为字符串。

在Python中,不能将字符串添加到int中。为此,可以使用不同的方法,如
format

a = input("enter the string value")
b = int(input("enter the number"))
c = "{}{}".format(a, b)
format
函数将对象作为参数,并通过对象的
str
表示来表示它们

在Python3.6及更高版本中,您可以使用
f-string
,通过在字符串之前添加
f
,并在字符串内部添加参数,与
format
的作用相同,如:

c = f'{a}{b}'
这两个选项都将在
c
中存储
a
b
的串联


使用
打印
功能还有另一个选项,如:

print(a, b, sep="")
print
函数接受由
分隔的所有参数,并打印对象的
str
表示形式,就像
格式
一样。默认情况下,打印的
sep
选项是在参数之间打印
”的空格。通过将其更改为
,它将按顺序打印参数,中间不留空格


使用此选项时,无需将
a
b
的串联存储在另一个变量中,即
c

在python中,不能添加不同类型(int、float、boolean等)的字符串值。 若要获得此代码的结果,必须在字符串类型或int类型中更改其中一个

a=input()
b=input()
c=a+b
print(c)


这回答了你的问题吗?如果一开始就不转换为
int
,不是更简单吗,因为
input
已经返回了一个字符串…数字的健全性检查如果
c
只是为了打印而计算的,那么也可以改为
print(a,b,sep='')
没错,我刚才解释了将int和string的值一起存储在一个变量中。当然!你做得很好,只是想把它作为另一个选项,而不是对你的答案提出批评。我在这里添加了很多建议。你很好地解释了如何将它保存到变量中,就像OP在问题中所做的那样。我的意思是,如果
c
不是真正必要的,并且只是为
打印创建的,那么这是一个更简单的选择:)
 a=int(input("enter the number"))
    b=int(input("enter the number"))
    c=a+b
    print(c)