iT邦幫忙

1

string index out of range

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20230319/20155809n9Ml0e6kU3.pnghttps://ithelp.ithome.com.tw/upload/images/20230319/20155809iflzxSpLQG.png

小弟在練習時遇到這個問題 麻煩各位大神前輩救我一下

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
海綿寶寶
iT邦大神 1 級 ‧ 2023-03-19 12:08:29
最佳解答

https://ithelp.ithome.com.tw/upload/images/20230319/20001787VXrktSTPKN.png

看更多先前的回應...收起先前的回應...
4A9G0093 iT邦新手 5 級 ‧ 2023-03-19 12:17:30 檢舉

他在跑解答時還是錯誤 同一個原因

你有改程式碼嗎?

4A9G0093 iT邦新手 5 級 ‧ 2023-03-19 12:30:15 檢舉

https://ithelp.ithome.com.tw/upload/images/20230319/20155809fMOWd2sbXJ.png
有的

不,你沒改

    g+=i
    p-=i

要改成

    g+=1
    p-=1
4A9G0093 iT邦新手 5 級 ‧ 2023-03-19 15:28:35 檢舉

感謝大神 可以請問一下原因嗎

題目是「判斷數字是否是迴文(正唸反唸都一樣)」
以 88888 的字元陣列為例
其位置分別為 a[0]a[1]a[2]a[3]a[4]
所以 g 從 0 開始, 由小到大, 每次加 1, 就是 0,1,2,3,4
而 p 從 4 開始, 由大到小, 每次減 1, 就是 4,3,2,1,0
如此一來, a[g]!=a[p]就可組合出
a[0]!=a[4]
a[1]!=a[3]
a[2]!=a[2]
a[3]!=a[1]
a[4]!=a[0]
幾種比較
而只要其中一次比較成立(即 a[g]!=a[p])
就回傳 false

froce iT邦大師 1 級 ‧ 2023-03-20 14:00:44 檢舉

str(x) == str(x)[::-1]

這樣寫比較像python的作風。

我要發表回答

立即登入回答