'tuple'对象没有属性'rstrip'python

lnj*_*lue 1 python arrays int split input

我正在编写一个python程序,它接受一个后来排序的int数组的用户输入.我已经在我的机器上成功编译了我的程序,然而,无法在Unix服务器上正确编译它.我的机器上的Python编译器是版本3,而我相信服务器可能在Python 2.6上运行.我不确定底层问题是什么.

list = input('Enter numbers in array with commas: ').rstrip() #this line is being flagged
list = list.split(',')
print(list)
Run Code Online (Sandbox Code Playgroud)

我的错误:

 AttributeError: 'tuple' object has no attribute 'rstrip'
Run Code Online (Sandbox Code Playgroud)

tim*_*geb 6

您正在使用Python2,其中input()相当于eval(raw_input()).所以你实际上是在评估你的输入(我认为是这样的1, 2)是一个元组 - 它没有rstrip属性.

使用raw_input而不是修复代码input.这将为您提供一个可以使用的字符串rstrip.

我还建议您使用另一个变量名来代替,list因为您将隐藏内置列表.