鱼C论坛

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

用字典做一个单词记录本,出现了逻辑错误(大概),求大神不吝赐教呀

[复制链接]
发表于 2020-9-28 21:08:33 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 杀手达文西 于 2020-9-28 21:13 编辑

#words-first

import time as t
import pickle as pkl


time = t.strftime('%Y-%m-%d')
ok = 1
while ok == 1:
    new_word = input('请输入你要记录的单词(直接回车退出单词本)\n')
    f = open(r'E:/pytest/words_first.pkl','ab+')
    f.seek(0)
    wb = pkl.load(f)

    #输入已保存单词/词组
    if new_word in wb:
        print('单词已存在')

    #输入回车键,跳出循环
    elif new_word == '':
        break

    #输入新单词
    else:
        wb[new_word]=(time)

    print(wb)
    print('已记录 %d 个单词/词组'%(len(wb)))

pkl.dump(wb,f)
f.close()
            

input('按任意键退出程序')
        
=======================================================================
这就是代码
然后是运行结果
======================= RESTART: E:/pytest/words_first.py ======================
请输入你要记录的单词(直接回车退出单词本)
hello
单词已存在
{'hello': '2020-09-28'}
已记录 1 个单词/词组
请输入你要记录的单词(直接回车退出单词本)
love
{'hello': '2020-09-28', 'love': '2020-09-28'}
已记录 2 个单词/词组
请输入你要记录的单词(直接回车退出单词本)
ooo
{'hello': '2020-09-28', 'ooo': '2020-09-28'}
已记录 2 个单词/词组
请输入你要记录的单词(直接回车退出单词本)

按任意键退出程序
===========================================================
新输入的单词被覆盖了,这是为啥呢?
(在这主程序之前我也写过一段代码记录单词本的第一个单词,后面的主程序就直接用的这个文件。之前那个程序和结果在下面)


import time as t
import pickle as pkl

with open(r'E:/pytest/words_first.pkl','wb') as f:
    time = t.strftime('%Y-%m-%d')
    dict1 = {'hello':(time)}
    pkl.dump(dict1,f)

with open(r'E:/pytest/words_first.pkl','rb') as f:
    n = pkl.load(f)
    for k,v in n.items():
        print('%s    %s'%(k,v))
        
==============================================================
运行结果:
hello    2020-09-28

最后求求大神帮帮忙啦![/img]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-28 21:23:22 | 显示全部楼层
运行后,words_first.pkl里面是空的,直接就报错了。。。。。。。
这个里面的内容是怎么存储的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-28 22:05:02 | 显示全部楼层
本帖最后由 kylin121380 于 2020-9-28 22:13 编辑
  1. import time as t
  2. import pickle as pkl
  3. import os

  4. time = t.strftime('%Y-%m-%d')
  5. ok = 1
  6. while ok:
  7.     new_word = input('请输入你要记录的单词(直接回车退出单词本)\n')
  8.     try:
  9.         if 'words_first.txt' not in os.listdir('.'):
  10.             with open(r'words_first.txt', 'wb') as f:
  11.                 pass
  12.         with open(r'words_first.txt', 'rb') as f:
  13.             f.seek(0)
  14.             wb = pkl.load(f)
  15.             print(wb)
  16.         # 输入已保存单词/词组
  17.         if new_word in wb:
  18.             print('单词已存在')

  19.         # 输入回车键,跳出循环
  20.         elif new_word == 'q':
  21.             break
  22.         # 输入新单词
  23.         else:
  24.             wb[new_word] = (time)
  25.             with open(r'words_first.txt', 'wb') as f:
  26.                 pkl.dump(wb, f)

  27.     except EOFError:
  28.         with open(r'words_first.txt', 'wb') as f:
  29.             dict1 = {new_word: (time)}
  30.             pkl.dump(dict1, f)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 19:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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