鱼C论坛

 找回密码
 立即注册
查看: 1526|回复: 2

[已解决]列表下标为整数

[复制链接]
发表于 2021-10-24 15:02:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def pig_latin(s):
  2.     text = []
  3.     for i in s:
  4.         if i[0] == 'a' or i[0] == 'e' or i[0] == 'i' or i[0] == 'o' or i[0] == 'u':
  5.             i += 'way'
  6.             text.append(i)
  7.         else:
  8.             n = 1
  9.             while 1:
  10.                 first = i[0, n]
  11.                 if i[n] == 'a' or i[n] == 'e' or i[n] == 'i' or i[n] == 'o' or i[n] == 'u':
  12.                     i = i[n:]
  13.                     i = i + first + 'ay'
  14.                     text.append(i)
  15.                     break
  16.                 n += 1
  17.     return text


  18. def main():
  19.     s = []
  20.     word = str(input('please enter a sentence(with no punctuation):'))
  21.     while word != ' ':
  22.         s.append(word)
  23.         word = str(input('please enter a sentence(with no punctuation):'))
  24.     text = pig_latin(s)
  25.     print('The result is:')
  26.     print(*text, sep=' ')


  27. if __name__ == '__main__':
  28.     main()
复制代码


第十行,n和0都已经是整数了,为什么还报错?
最佳答案
2021-10-25 08:14:26
取列表元素目前只支持:
1)单index,如list[0]
2)切片index,如list[0:10]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-25 08:14:26 | 显示全部楼层    本楼为最佳答案   
取列表元素目前只支持:
1)单index,如list[0]
2)切片index,如list[0:10]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-25 17:56:11 | 显示全部楼层
first = i[0: n]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 16:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表