我在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