鱼C论坛

 找回密码
 立即注册
查看: 1545|回复: 8

[已解决]某小学生的期中复习答题程序出错

[复制链接]
发表于 2017-11-5 10:25:55 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 1314xxxxxx 于 2017-11-5 10:35 编辑

代码如下:
  1. #It is a good program for Chinese student.
  2. def chuli_input (rightAnswer,inputAnswer):
  3.     true_str = 0
  4.     false_str = 0
  5.     output = []
  6.     if len(rightAnswer) != len(inputAnswer):     #input is not right.
  7.         return False
  8.     else:
  9.         for i in range(len(rightAnswer)):
  10.             if rightAnswer[i] == inputAnswer[i]:
  11.                 true_str += 1
  12.             else:
  13.                 false_str += 1
  14.         output[0] = true_str
  15.         output[1] = false_str
  16.         return output

  17. def print_question (question_text):
  18.     answer = []
  19.     text = ""
  20.     for i in range(len(question_text)):
  21.         text += question_text[i] + " " + str(i+1) + " "
  22.     print(text)
  23.     for i in range(len(question_text)):
  24.         answer[i] = input("%s:" % (str(i+1)))
  25.     return answer

  26. first_rightAnswer = ["构思新奇、富有想象力、充满好奇心","带着满怀的好心情","一个如诗如画的自然境界","对大自然的热爱之情"]
  27. second_rightAnswer = ["独特的感受、奇特的想象,用清新生动的语言","有声有色的山林雨景图","山雨","大自然","喜爱"]
  28. third_rightAnswer = ["别具一格的观察力、想象力和创造力","奇异的游历","热爱自然的美好情操","晶莹透彻的童心","浓浓的生活气息"]
  29. fourth_rightAnswer = ["美丽的索溪峪",'山、水、动物、游人']
  30. all_rightAnswer = [first_rightAnswer,second_rightAnswer,third_rightAnswer,fourth_rightAnswer]

  31. first_question_text = ["《山中访友》是一篇" , "的散文。文章主要写作者"","",走进山林,探访了山中的古桥、树林、山泉、溪流、瀑布等,与这些朋友们互诉心声,营造了" , ",表达了"]
  32. second_question_text = ["在《山雨》中,作者以" , "向读者展示了一幅," , "字里行间洋溢着" ]
  33. third_question_text = ["《草虫的村落》一文中作者以," , "为我们描绘了这样一次," , "使我们感受到作者在神游这个想象世界中所透射出来的," , "感受到," , "感受到字里行间洋溢着的"]
  34. fourth_question_text = ["课文通过作者身处" , ",感受到一个野字,然后具体向我们介绍了" , "的野"]

  35. first_inputAnswer = print_question(first_question_text)
  36. second_inputAnswer = print_question(second_question_text)
  37. third_inputAnswer = print_question(third_question_text)
  38. fourth_inputAnswer = print_question(fourth_question_text)
  39. all_inputAnswer = [first_inputAnswer,second_inputAnswer,third_inputAnswer,fourth_inputAnswer]

  40. exam = ""

  41. for i in range(4):
  42.     for a in range(len(all_inputAnswer[i])):
  43.         output = chuli_input(all_rightAnswer[i][a],all_inputAnswer[i][a])
  44.         exam += "对了" + str(output[0]) + "个字符," + "错了" + str(output[1]) + "个字符" + "\n"
复制代码

错误在25行,错误信息如下:
Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/fuxi.py", line 39, in <module>
    first_inputAnswer = print_question(first_question_text)
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/fuxi.py", line 25, in print_question
    answer = input("%s:" % (str(i+1)))
IndexError: list assignment index out of range
@小甲鱼 召唤小甲鱼老师!(希望快点来,不,能成功就好)
最佳答案
2017-11-5 10:36:17
报错已经说了,列表索引超过范围
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-5 10:36:17 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +4 鱼币

报错已经说了,列表索引超过范围
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-5 10:37:38 | 显示全部楼层
小锟 发表于 2017-11-5 10:36
报错已经说了,列表索引超过范围

那为什么会超过范围呢?Python列表有限制的吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-5 10:38:42 | 显示全部楼层
1314xxxxxx 发表于 2017-11-5 10:37
那为什么会超过范围呢?Python列表有限制的吗?

你列表是空列表啊,索引多少都是超过范围的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-11-5 10:38:51 | 显示全部楼层
1314xxxxxx 发表于 2017-11-5 10:37
那为什么会超过范围呢?Python列表有限制的吗?

好了,没事了。
把answer列表的初始化改成[""]就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-5 10:40:51 | 显示全部楼层
小锟 发表于 2017-11-5 10:38
你列表是空列表啊,索引多少都是超过范围的

那个应该是append,而不是赋值,对吧?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-5 10:41:58 | 显示全部楼层
1314xxxxxx 发表于 2017-11-5 10:40
那个应该是append,而不是赋值,对吧?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-5 10:49:26 | 显示全部楼层
小锟 发表于 2017-11-5 10:36
报错已经说了,列表索引超过范围

Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/fuxi.py", line 59, in <module>
    exam += "对了" + str(output[0]) + "个字符," + "错了" + str(output[1]) + "个字符" + "\n"
TypeError: 'bool' object is not subscriptable

那这是为什么呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-5 10:59:02 | 显示全部楼层
1314xxxxxx 发表于 2017-11-5 10:49
Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/P ...

布尔值没有下标,你的这个函数有可能是返回布尔值的,布尔值不是字符串或者列表,所以是没有索引的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 11:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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