[英]Python-3.x: while using the following code i got an error as “object has no attribute 'split ”'
n = int(input())
arr = [int(x) for x in input.split()]
this is the code i used. 这是我使用的代码。 I want to get list of input from the user but i am getting error as
我想从用户那里获取输入列表,但由于出现错误
arr = [int(x) for x in input.split()]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
You are calling split on the builtin function input
. 您正在对内置函数
input
调用split。 While what you should be doing is calling it on the n
variable. 您应该在
n
变量上调用它。 So it should look like this 所以应该看起来像这样
n = int(input())
arr = [int(x) for x in n.split()] # Still wrong
But still let me quote that int
however has no split attribute. 但是我还是要引用
int
但是没有split属性。 So make sure it is a String type. 因此,请确保它是String类型。
So your try not to convert your input into int
until you process the value. 因此,在处理该值之前,请勿尝试将输入转换为
int
。
You're trying to split the input function itself, and not its results. 您试图拆分输入函数本身,而不是结果。 You need to split the string it returns, like this
input().split()
. 您需要分割返回的字符串,例如此
input().split()
。 Mind you that this will not do anything because split has no parameters. 请注意,这不会做任何事情,因为split没有参数。 It'll simply return a list with the user's input as the sole parameter.
它只会返回一个列表,其中用户输入是唯一的参数。 If you want to divide the input string into its chars, you can simply use
list(input())
. 如果要将输入字符串分成其字符,则可以简单地使用
list(input())
。
I believe this is what you are going for, turn the input to a list and ints for items in the list 我相信这就是您要的目的,将输入内容转换为列表,并为列表中的项目输入整数
l = [int(i) for i in list(input('Enter number: '))]
Enter number: 101 [1, 0, 1]
As Vineeth said, you are trying to call split on input. 正如Vineeth所说,您正在尝试在输入时调用split。 Also, you can't split an integer since split is a method that can only be used on strings, so you shouldn't cast n as an integer.
另外,您不能拆分整数,因为split是只能在字符串上使用的方法,因此不应将n强制转换为整数。
n = str(input())
arr = [int(x) for x in n.split()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.