每当我运行is_palindrome('otto')时,它都会显示错误'IndexError:字符串索引超出范围'...但是,当我只编译代码时,它不会显示任何错误。感谢您的帮助
@override
build(Buildcontext context) {
Stack (
diceRoll == 1 ?
Container(
BoxDecoration(
image: ('lib/images/dice_image_1.jpg),
),
) : diecroll == 2?
Container(
BoxDecoration(
image: ('lib/images/dice_image_2.jpg),
),
) : diecroll == 3? // etc etc etc. 20 times!!
);
}
答案 0 :(得分:5)
正在发生的是,对于偶数个字符的字符串,它将最终运行palindrome(“”),这将引发超出范围的错误。尝试切换len(word)<=1
和word[0] != word[-1]