Python 类型错误:';int';对象在分析用户输入时不可编辑 目标:输入列表[2,7]接收输出[2,7,3] print('要求输入前两个数字时键入27') response=int(输入('列表的前两个数字?')) 开始\列表=列表(响应) 打印('列表的前两个数字是',开始列表) 如果开始列表[0]

Python 类型错误:';int';对象在分析用户输入时不可编辑 目标:输入列表[2,7]接收输出[2,7,3] print('要求输入前两个数字时键入27') response=int(输入('列表的前两个数字?')) 开始\列表=列表(响应) 打印('列表的前两个数字是',开始列表) 如果开始列表[0],python,input,Python,Input,我认为错误在于以下代码: start_list = list(response) 您应该将您的响应置于[]之间,如: start_list = list([response]) 我认为错误在于此代码: start_list = list(response) 您应该将您的响应置于[]之间,如: start_list = list([response]) 我猜您希望用户输入两个由空格分隔的整数。你肯定不会这么做。首先,对看起来像“123 456”的输入应用int不是正确的方法。您需要str.

我认为错误在于以下代码:

start_list = list(response)
您应该将您的响应置于[]之间,如:

start_list = list([response])

我认为错误在于此代码:

start_list = list(response)
您应该将您的响应置于[]之间,如:

start_list = list([response])

我猜您希望用户输入两个由空格分隔的整数。你肯定不会这么做。首先,对看起来像
“123 456”
的输入应用
int
不是正确的方法。您需要
str.split
,然后将每个元素转换为整数。这方面的常见习惯用法是:

x, y = map(int, input('Enter two numbers: ').split())
Enter two numbers: 123 456

print(x, y)
(123, 456)
在此之后,您还可以将您的号码放入列表中:

start_list = [x, y]
或者,如果您不想解压缩
映射
结果,可以使用:

start_list = list(map(int, input('Enter two numbers: ').split()))
如果您正在使用python2,请省略
列表



请注意,如果用户输入的数字多于或少于两个以空格分隔的数字,则解包不是一个好主意。在这种情况下,最好不要首先解压缩结果。

我猜您希望用户输入两个由空格分隔的整数。你肯定不会这么做。首先,对看起来像
“123 456”
的输入应用
int
不是正确的方法。您需要
str.split
,然后将每个元素转换为整数。这方面的常见习惯用法是:

x, y = map(int, input('Enter two numbers: ').split())
Enter two numbers: 123 456

print(x, y)
(123, 456)
在此之后,您还可以将您的号码放入列表中:

start_list = [x, y]
或者,如果您不想解压缩
映射
结果,可以使用:

start_list = list(map(int, input('Enter two numbers: ').split()))
如果您正在使用python2,请省略
列表



请注意,如果用户输入的数字多于或少于两个以空格分隔的数字,则解包不是一个好主意。在这种情况下,最好不要首先解压缩结果。

如果要接受2个数字,可能需要使用原始输入

response = raw_input('first two numbers of the list?  ') # this will give you a string like '1 2'
然后,您希望将其转换为整数列表

start_list = [int(x) for x in response.split()]

然后,您的其他代码应该可以正常工作

如果您想要接受2个数字,您可能需要使用原始输入

response = raw_input('first two numbers of the list?  ') # this will give you a string like '1 2'
然后,您希望将其转换为整数列表

start_list = [int(x) for x in response.split()]

然后,您的其他代码应该可以正常工作

您可以修改列表的生成方式

print('when asked for first two numbers type 27')
response = input('first two numbers of the list?  ')
start_list = list(map(int, response.split()))
print('first two numbers of the list are', start_list)
if start_list[0] < start_list[1]:
    new = start_list[0] + 1
    start_list.insert(2, new)
print('first algo run gives', start_list)

您可以修改列表的制作方式

print('when asked for first two numbers type 27')
response = input('first two numbers of the list?  ')
start_list = list(map(int, response.split()))
print('first two numbers of the list are', start_list)
if start_list[0] < start_list[1]:
    new = start_list[0] + 1
    start_list.insert(2, new)
print('first algo run gives', start_list)

使用coldspeed的建议,并认识到
split()
返回字符串列表

然后记住将字符串转换回int,这似乎解决了我的系统上的问题:

start_list = list(input('Enter two numbers: ').split())
print('first two numbers of the list are', start_list)
if start_list[0] < start_list[1]:
    new = int(start_list[0]) + 1  # need to convert first string to an int with a call to int()
    start_list.insert(2, new)
print('first algo run gives', start_list)
start\u list=list(输入('输入两个数字:')。split()
打印('列表的前两个数字是',开始列表)
如果开始列表[0]<开始列表[1]:
new=int(start_list[0])+1#需要通过调用int()将第一个字符串转换为int
开始列表。插入(2,新)
打印('第一次算法运行',开始列表)

使用coldspeed的建议,并认识到
split()
返回字符串列表

然后记住将字符串转换回int,这似乎解决了我的系统上的问题:

start_list = list(input('Enter two numbers: ').split())
print('first two numbers of the list are', start_list)
if start_list[0] < start_list[1]:
    new = int(start_list[0]) + 1  # need to convert first string to an int with a call to int()
    start_list.insert(2, new)
print('first algo run gives', start_list)
start\u list=list(输入('输入两个数字:')。split()
打印('列表的前两个数字是',开始列表)
如果开始列表[0]<开始列表[1]:
new=int(start_list[0])+1#需要通过调用int()将第一个字符串转换为int
开始列表。插入(2,新)
打印('第一次算法运行',开始列表)

我看到你在学习Python。以下是我的建议:

让用户准确输入两个数字的最好方法是提示他两次。函数
input
返回一个字符串,您可以将其转换为int(正如您所做的那样)

将这两个值放入一个列表中。最简单的方法是使用
[…]
语法:

start_list = [response1, response2]
print('first two numbers of the list are', start_list)
现在附加第三个元素

if start_list[0] < start_list[1]:
    new_value = start_list[0] + 1  # don't use the name new
    start_list.append(new_value)  # append always adds it at the end
print('first algo run gives', start_list)  # what's an algo?
如果开始列表[0]
名称
new
不是变量的好名称,因为它有特殊的含义


如果您知道希望将值放在列表的末尾,请使用
append
而不是
insert

我看你在学蟒蛇。以下是我的建议:

让用户准确输入两个数字的最好方法是提示他两次。函数
input
返回一个字符串,您可以将其转换为int(正如您所做的那样)

将这两个值放入一个列表中。最简单的方法是使用
[…]
语法:

start_list = [response1, response2]
print('first two numbers of the list are', start_list)
现在附加第三个元素

if start_list[0] < start_list[1]:
    new_value = start_list[0] + 1  # don't use the name new
    start_list.append(new_value)  # append always adds it at the end
print('first algo run gives', start_list)  # what's an algo?
如果开始列表[0]
名称
new
不是变量的好名称,因为它有特殊的含义


如果您知道希望将值放在列表的末尾,请使用
append
而不是
insert

这可能会解决一个错误,但我无法想象这会真正帮助OP。这可能会在以后导致另一个错误。这可能会解决一个错误,但我无法想象这会真正帮助OP。这可能会在以后导致另一个错误。假设用户给你一个
1
。你期望
start\u list=list(response)
能做什么?如果