Skip to main content
 首页 » 编程设计

python之“元组”对象没有属性 'rstrip' python

2025年05月04日18pengyingh

我正在编写一个 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) 

我的错误:

 AttributeError: 'tuple' object has no attribute 'rstrip' 

请您参考如下方法:

您正在使用 Python2,其中 input() 等同于 eval(raw_input())。因此,您实际上是在评估您的输入(我认为它类似于 1, 2)是一个元组 - 它没有 rstrip 属性。

使用 raw_input 而不是 input 来修复您的代码。这将为您提供一个字符串,您可以在其上使用 rstrip。

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