|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
制作英文学习字典。编写程序制作英文学习字典,词典有3个基本功能:添加、查询和退出。程序读取源文件路径下的txt格式词典文件,若没有就创建一个。词典文件存储方式为“英文单词 中文单词”。每行仅有一对中英释义。程序会根据用户选择进相应的功能模块,并显示相应的操作提示。当添加的单词已存在时,显示“该单词已添加到字典库”;当查询的单词不存在时,显示“字典库中未找到这个单词”。用户输入其他选项时,提示“输入有误”。
代码如下:
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import json
Dict = {}
path = "dict.txt"
# 读文件
def read_file(path):
try:
with open(path, "r", encoding="GBK") as fp:
Dict = json.load(fp)
except FileNotFoundError:
with open(path, "w", encoding="GBK") as fp:
pass
return fp
# 更新文件
def update_file(path):
with open(path, "w", encoding="GBK") as fp:
for english in Dict:
chinese = Dict[english]
fp.write(f"{english}{chinese}\n")
# 添加单词
def add_word():
while True:
word = input("请输入要添加的英文单词:")
if word in Dict:
print("该单词已添加到字典库")
break
chinese = input("请输入该单词的中文意思:")
Dict[word] = chinese
update_file(path)
print("添加成功")
# 查询单词
def query_word():
while True:
word = input("请输入要查询的英文单词:")
if word in Dict:
print(Dict[word])
break
else:
print("字典库中未找到这个单词")
# 主函数
def main():
read_file(path)
while True:
options = input("请选择操作:1.添加单词 2.查询单词 3.退出程序")
if options == "1":
add_word()
elif options == "2":
query_word()
elif options == "3":
break
else:
print("输入有误,请重新输入")
选择默认配置,点击run,运行不了,且出现runfile('C:/Users/a_che/.spyder-py3/temp.py', wdir='C:/Users/a_che/.spyder-py3'),点击右键run cell,则出现runcell(0, 'C:/Users/a_che/.spyder-py3/temp.py')
求助求助!!谢谢大佬们!!
|
|