鱼C论坛

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

[已解决]我想做个背单词的小程序,但二次答题那段有bug(望大神指点)

[复制链接]
发表于 2020-4-25 19:34:03 | 显示全部楼层 |阅读模式

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

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

x
我想做个背单词的小程序,但二次答题那段有bug(望大神指点迷津
  1. import os
  2. import time as g
  3. dan_ci=[]
  4. fan_yi=[]
  5. z1=0
  6. def jing_du_tiao(a):
  7.     global z1
  8.     os.system('cls')
  9.     z1+=100/a
  10.     print(str(int(z1+0.5))+'%')
  11. def jia(a,b):
  12.     global dan_ci,fan_yi
  13.     dan_ci.append(b)
  14.     fan_yi.append(a)
  15.     jing_du_tiao(23)
  16. os.system('color 5F')
  17. print('0%')
  18. jia('强盗','robber')
  19. jia('抢劫','rob')
  20. jia('国王','king')
  21. jia('扮演,行动','act')
  22. jia('合理的','reasonable')
  23. jia('富于想象的','imaginative')
  24. jia('想象力','imagination')
  25. jia('理由','reason')
  26. jia('有能力的','able')
  27. jia('配音,配乐','dub')
  28. jia('疑惑','confused')
  29. jia('乐团','musical')
  30. jia('音乐','music')
  31. jia('惊奇,太好了','amazing')
  32. jia('不同的','different')
  33. jia('较年轻的','younger')
  34. jia('年轻的','young')
  35. jia('最年轻的','youngest')
  36. jia('最新鲜的','freshest')
  37. jia('比较新鲜的','fresher')
  38. jia('新鲜的','fresh')
  39. jia('腊肠','sausage')
  40. jia('休息','rest')
  41. os.system('color 5F')
  42. cuo_wu=[]
  43. for i in range(len(dan_ci)):
  44.     os.system('cls')
  45.     da_an=input(fan_yi[i]+':')
  46.     if da_an != dan_ci[i]:
  47.         cuo_wu.append(dan_ci[i])
  48. os.system('cls')
  49. if len(cuo_wu) > 0:
  50.     os.system('color 67')
  51.     print('你错了:',end='')
  52.     for i in cuo_wu:
  53.         print(i,end='、')
  54.     g.sleep(len(cuo_wu)*5)
  55.     while len(cuo_wu) > 0:#二次答题
  56.         new_cuo_wu=[]
  57.         os.system('color 5F')
  58.         for i in cuo_wu:
  59.             os.system('cls')
  60.             da_an=input(fan_yi[dan_ci.index(i)]+':')#中英转换
  61.             if da_an != i:
  62.                 new_cuo_wu.append(i)
  63.         if len(new_cuo_wu) < 1:
  64.             break
  65.         else:
  66.             cuo_wu=new_cuo_wu#避免无法删除对的
  67.         os.system('color 67')
  68.         print('你错了:',end='')
  69.         for i in cuo_wu:
  70.             print(i,end='、')
  71.         g.sleep(10)
  72.         os.system('cls')
  73.     os.system('cls')
  74.     os.system('color EC')
  75.     print('你终于就把单词背下来了。')
  76. else:
  77.     os.system('color EC')
  78.     print('你一次就把单词背下来了,真棒!')
复制代码

这是源代码。
最佳答案
2020-4-26 10:06:25
本帖最后由 _2_ 于 2020-4-26 10:26 编辑

我给你重写了一个,你看看行不行:

  1. import random as r

  2. class ReciteWords:
  3.     def __init__(self, **args):
  4.         """
  5.         You shoud input like English='Chinese'.
  6.         For example, ReciteWords(my='我的', you='你的')
  7.         """
  8.         self.wordsDict = {**args}
  9.         self.EnglishWords = list(self.wordsDict.keys())

  10.     def start(self):
  11.         """
  12.         Start recite words.
  13.         """
  14.         used = []
  15.         unused = self.EnglishWords
  16.         wrong = []
  17.         times = 0
  18.         total_times = len(unused)
  19.         isWrong = False
  20.         while unused:
  21.             a = r.choice(unused)
  22.             if input("(%d / %d)请输入 %s 的意思:" % (times, total_times, a)) == self.wordsDict.get(a):
  23.                 print("恭喜你, 答对了!")
  24.                 used.append(unused.pop(unused.index(a)))
  25.                 times += 1
  26.                 continue
  27.             else:
  28.                 print("正确答案是: %s" % self.wordsDict.get(a))
  29.                 used.append(unused.pop(unused.index(a)))
  30.                 wrong.append(used[-1])
  31.                 isWrong = True
  32.                 times += 1
  33.                 continue

  34.         print("测试结束!")
  35.         print("你本次累计练习 %d 题!" % total_times)
  36.         if isWrong:
  37.             print("你出现了一些错误")
  38.             print("你出现的错误有: ", [i for i in wrong])
  39.             print("继续努力!")

  40.         input()

  41.         return None

  42. if __name__ == "__main__":
  43.     a = ReciteWords(robber="强盗",rob="抢劫",king="国王",act="扮演",reasonable="合理的",imaginative="富于想象的",imagination="想象力",reason="理由",able="有能力的",dub"配音",confused="疑惑",musical="乐团",music="音乐",amazing="惊奇",different="不同的",younger="较年轻的",young="年轻的",youngest="最年轻的",freshest="最新鲜的",fresher="比较新鲜的",fresh="新鲜的",sausage="腊肠",rest="休息")
  44.     a.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-25 19:45:03 | 显示全部楼层
字典会吗,(建议用字典)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-25 19:50:15 | 显示全部楼层
在程序的末尾加一个 input() 就可以了
Python FAQ 017 双击运行程序闪退
https://fishc.com.cn/thread-159173-1-1.html
(出处: 鱼C论坛)

  1. import os
  2. import time as g
  3. dan_ci=[]
  4. fan_yi=[]
  5. z1=0
  6. def jing_du_tiao(a):
  7.     global z1
  8.     os.system('cls')
  9.     z1+=100/a
  10.     print(str(int(z1+0.5))+'%')
  11. def jia(a,b):
  12.     global dan_ci,fan_yi
  13.     dan_ci.append(b)
  14.     fan_yi.append(a)
  15.     jing_du_tiao(23)
  16. os.system('color 5F')
  17. print('0%')
  18. jia('强盗','robber')
  19. jia('抢劫','rob')
  20. jia('国王','king')
  21. jia('扮演,行动','act')
  22. jia('合理的','reasonable')
  23. jia('富于想象的','imaginative')
  24. jia('想象力','imagination')
  25. jia('理由','reason')
  26. jia('有能力的','able')
  27. jia('配音,配乐','dub')
  28. jia('疑惑','confused')
  29. jia('乐团','musical')
  30. jia('音乐','music')
  31. jia('惊奇,太好了','amazing')
  32. jia('不同的','different')
  33. jia('较年轻的','younger')
  34. jia('年轻的','young')
  35. jia('最年轻的','youngest')
  36. jia('最新鲜的','freshest')
  37. jia('比较新鲜的','fresher')
  38. jia('新鲜的','fresh')
  39. jia('腊肠','sausage')
  40. jia('休息','rest')
  41. os.system('color 5F')
  42. cuo_wu=[]
  43. for i in range(len(dan_ci)):
  44.     os.system('cls')
  45.     da_an=input(fan_yi[i]+':')
  46.     if da_an != dan_ci[i]:
  47.         cuo_wu.append(dan_ci[i])
  48. os.system('cls')
  49. if len(cuo_wu) > 0:
  50.     os.system('color 67')
  51.     print('你错了:',end='')
  52.     for i in cuo_wu:
  53.         print(i,end='、')
  54.     g.sleep(len(cuo_wu)*5)
  55.     while len(cuo_wu) > 0:#二次答题
  56.         new_cuo_wu=[]
  57.         os.system('color 5F')
  58.         for i in cuo_wu:
  59.             os.system('cls')
  60.             da_an=input(fan_yi[dan_ci.index(i)]+':')#中英转换
  61.             if da_an != i:
  62.                 new_cuo_wu.append(i)
  63.         if len(new_cuo_wu) < 1:
  64.             break
  65.         else:
  66.             cuo_wu=new_cuo_wu#避免无法删除对的
  67.         os.system('color 67')
  68.         print('你错了:',end='')
  69.         for i in cuo_wu:
  70.             print(i,end='、')
  71.         g.sleep(10)
  72.         os.system('cls')
  73.     os.system('cls')
  74.     os.system('color EC')
  75.     print('你终于就把单词背下来了。')
  76. else:
  77.     os.system('color EC')
  78.     print('你一次就把单词背下来了,真棒!')

  79. input()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-25 21:03:59 | 显示全部楼层
我不是那个意思,我是说二次答题的感觉不好,不信你直接双击运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-25 21:09:00 | 显示全部楼层
本帖最后由 悠悠2264 于 2020-4-25 21:18 编辑

在每个print里加一个参数flush即可,这个函数可以使在cmd里能动态刷新(不设置则遇到换行符或缓冲区满了或程序结束才会打印到cmd),而在IDLE则无需添加此参数。如:
  1. print('你错了:', end=''", flush=True)
复制代码

这样就可正常输出到cmd了,所以把每个print后加:   , flush=True   即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-25 22:17:15 From FishC Mobile | 显示全部楼层
干嘛用的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-26 10:06:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 _2_ 于 2020-4-26 10:26 编辑

我给你重写了一个,你看看行不行:

  1. import random as r

  2. class ReciteWords:
  3.     def __init__(self, **args):
  4.         """
  5.         You shoud input like English='Chinese'.
  6.         For example, ReciteWords(my='我的', you='你的')
  7.         """
  8.         self.wordsDict = {**args}
  9.         self.EnglishWords = list(self.wordsDict.keys())

  10.     def start(self):
  11.         """
  12.         Start recite words.
  13.         """
  14.         used = []
  15.         unused = self.EnglishWords
  16.         wrong = []
  17.         times = 0
  18.         total_times = len(unused)
  19.         isWrong = False
  20.         while unused:
  21.             a = r.choice(unused)
  22.             if input("(%d / %d)请输入 %s 的意思:" % (times, total_times, a)) == self.wordsDict.get(a):
  23.                 print("恭喜你, 答对了!")
  24.                 used.append(unused.pop(unused.index(a)))
  25.                 times += 1
  26.                 continue
  27.             else:
  28.                 print("正确答案是: %s" % self.wordsDict.get(a))
  29.                 used.append(unused.pop(unused.index(a)))
  30.                 wrong.append(used[-1])
  31.                 isWrong = True
  32.                 times += 1
  33.                 continue

  34.         print("测试结束!")
  35.         print("你本次累计练习 %d 题!" % total_times)
  36.         if isWrong:
  37.             print("你出现了一些错误")
  38.             print("你出现的错误有: ", [i for i in wrong])
  39.             print("继续努力!")

  40.         input()

  41.         return None

  42. if __name__ == "__main__":
  43.     a = ReciteWords(robber="强盗",rob="抢劫",king="国王",act="扮演",reasonable="合理的",imaginative="富于想象的",imagination="想象力",reason="理由",able="有能力的",dub"配音",confused="疑惑",musical="乐团",music="音乐",amazing="惊奇",different="不同的",younger="较年轻的",young="年轻的",youngest="最年轻的",freshest="最新鲜的",fresher="比较新鲜的",fresh="新鲜的",sausage="腊肠",rest="休息")
  44.     a.start()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-27 22:04:46 | 显示全部楼层
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 07:42:34 From FishC Mobile | 显示全部楼层
磊之茶 发表于 2020-4-27 22:04
谢谢

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 12:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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