TypeError: Can only concatenate str (not "int") to str (简单的Python程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52225721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:02:59  来源:igfitidea点击:

TypeError: Can only concatenate str (not "int") to str (simple Python programme)

python

提问by Madushk

I wrote this simple code and tried to execute in Windows 10 CMD ... and it gets the error message :

我写了这个简单的代码并尝试在 Windows 10 CMD 中执行......它得到了错误消息:

TypeError: Can only concatenate str (not "int") to str

code :

代码 :

userName = input('Name: ')
age = input('age: ')

factor = 2

finalAge = age + factor

print('In', factor, 'years you will be', finalAge, 'years old', userName+'!')

I am using Python 3.7.0 in Windows 10

我在 Windows 10 中使用 Python 3.7.0

回答by SA12345

The input() command in line 2 of your code would turn any input provided by the user into a STRING. Therefore, when you try to add that STRING to a number (float or integer; in your case you have an integer i.e. factor=2) it won't (and shouldn't!) work.

代码第 2 行中的 input() 命令会将用户提供的任何输入转换为 STRING。因此,当您尝试将该 STRING 添加到数字(浮点数或整数;在您的情况下,您有一个整数,即 factor=2)时,它不会(也不应该!)起作用。

Therefore, for the + operation to continue, both the quantities to the left and right of that + sign must be of the same type (strings, or numbers)

因此,要继续执行 + 操作,+ 符号左侧和右侧的数量必须是相同的类型(字符串或数字)

回答by Faisal Abdul Rahiman Athikkai

TypeError: Can only concatenate str (not “int”) to str

类型错误:只能将 str(不是“int”)连接到 str

This Error states that it can only concatenate string to string.In your program you have tried to concatenate a sting and integer

此错误指出它只能将字符串连接到字符串。在您的程序中,您尝试连接一个字符串和整数

Input command would only fetch string from the user, and the age the user enters should be taken as string. Examples: '45', '25', 36 etc..

输入命令只会从用户那里获取字符串,用户输入的年龄应该作为字符串。示例:'45'、'25'、36 等。

This program is trying to concatenate '45' + 2 . Which throws this error.

该程序试图连接 '45' + 2 。这会引发此错误。

Instead you can try converting the user input to int and then concatenate.

相反,您可以尝试将用户输入转换为 int 然后进行连接。

userName = input('Name: ')
age = int(input('age: '))
finalAge = age + factor
factor = 2
finalAge = age + factor
print('In ', factor, 'years you will be', finalAge, 'years old', userName, '!')

回答by h h h

You add a string to an int, because the input()function returns a string. Use int(input('age: ')).

您将字符串添加到 int,因为该input()函数返回一个字符串。使用int(input('age: ')).

回答by Jonathan

Convert age to int to do your maths:

将 age 转换为 int 以进行数学运算:

finalAge = int(age) + factor

And as a bonus you could use the .format() option:

作为奖励,您可以使用 .format() 选项:

print("In {0} years you will be {1} years old, {2}!".format(factor, finalAge, userName))

回答by Melvin Abraham

In python, you cannot concatenate two completely different data types.

在 python 中,您不能连接两种完全不同的数据类型。

1) 1 + 1 = 2
2) '1' + '1' = '11' (string within quotes)
3) '1' + 1 = ??

Think about it...

想一想...

Well, in other programming languages like C, the integer would be converted into char(or character) and would undergo operation 2...

好吧,在C等其他编程语言中,整数将被转换为char(或字符)并进行操作 2...

So, in this case, you need to explicitly cast the str(or string) data type other than into an integer(or int) using the function int().

因此,在这种情况下,您需要使用函数将str(或字符串)数据类型显式转换为整数(或int)以外的类型int()

Syntax: int('<some string>')

句法: int('<some string>')

Example: int('7')would yield 7

示例:int('7')将产生7

So, you either need to take the input as integeror else convert the string to integer while computing finalAge:

因此,您要么需要将输入作为整数,要么在计算时将字符串转换为整数finalAge

age = int(input('age: '))

Or

或者

finalAge = int(age) + factor

回答by Melvin Abraham

In python, you cannot concatenate two completely different data types.

在 python 中,您不能连接两种完全不同的数据类型。

1) 1 + 1 = 2
2) '1' + '1' = '11' (string within quotes)
3) '1' + 1 = ??

Think about it...

想一想...

Well, in other programming languages like C, the integer would be converted into char(or character) and would undergo operation 2...

好吧,在C等其他编程语言中,整数将被转换为char(或字符)并进行操作 2...

So, in this case, you need to explicitly cast the str(or string) data type other than into an integer(or int) using the function int().

因此,在这种情况下,您需要使用函数将str(或字符串)数据类型显式转换为整数(或int)以外的类型int()

Syntax: int('<some string>')

句法: int('<some string>')

Example: int('7')would yield 7

示例:int('7')将产生7

So, you either need to take the input as integeror else convert the string to integer while computing finalAge:

因此,您要么需要将输入作为整数,要么在计算时将字符串转换为整数finalAge

age = int(input('age: '))

Or

或者

finalAge = int(age) + factor

回答by Chris Stephens

Python 3.7 this will do what you want.

Python 3.7 这会做你想做的。

userName = input('Name: ')
age = int(input('age: '))
factor = 2
finalAge = age + factor
print("In", + factor, "years you will be", + finalAge, "years old " + userName + "!")

回答by Mohit kumar

Python is strongly typed, so it does not do type coercion unless you tell it to.

Python 是强类型的,因此除非您告诉它,否则它不会执行类型强制。

You get that error if you try to add a number to a string, because based on the first operand it figures you want to concatenate strings.

如果您尝试向字符串添加数字,则会出现该错误,因为根据第一个操作数,它认为您想要连接字符串。

If you try to add a string to a number, you get “unsupported operand types” instead, but it's the same problem.

如果您尝试将字符串添加到数字,则会得到“不受支持的操作数类型”,但这是同样的问题。

If you want to turn a number-in-a-string into an int you can add, use int(). If you want to turn a number value into a string you can concatenate, use str().

如果要将字符串中的数字转换为可以添加的整数,请使用int(). 如果要将数字值转换为可以连接的字符串,请使用str().

回答by Start_over

I had the same problem with django and I solved it by clearing the chrome cache

我在 django 上遇到了同样的问题,我通过清除 chrome 缓存解决了这个问题

回答by Start_over

userName = input('Name: ') age = input('age: ')

用户名 = 输入('姓名:')年龄 = 输入('年龄:')

factor = 2

因子 = 2

finalAge = int(age) + factor

finalAge = int(age) + 因子

print('In', factor, 'years you will be', finalAge, 'years old', userName+'!')

print('In', factor, '你将成为的年数', finalAge, '岁数', userName+'!')