我在python中编写了下面的代码来检查第i个术语和第n-1个术语的相等性。我得到一个错误首先结束for循环(在:)。请帮助Python代码来检查第i个和第i-1个术语的相等性
arr=[]
n=int(input("Enter the number of terms you want in the array"))
for i in range(0,n):
a=int(input("Enter your number here"))
arr.append(a)
for i in range(0,len(arr)):
if arr[i]==arr[len(arr)-i-1]:
print("The "+i+"th element and the "+len(arr)-i-1+"th element are equal")
非常感谢您的回答。我仍然面临错误。我正在得到多余的答案。如第0和第4项相等,第4和第0项相等。如何消除这个问题? –
@joeylang根据新规格编辑我的回复。 – Jerrybibo
我相信你的编辑存在缺陷。如果最后两个元素相等,将范围减半,您将错过它。 – Flynsee