我该如何使它保存对词典的更改?它将能够添加一个定义,然后它将能够退出,然后仍然保留该定义
words = {
"orange": "naranja",
"hello": "hola",
"bye": "adiós",
"red": "rojo",
"blue": "azul",
"yellow": "amarillo",
"purple": "púrpura",
"green": "verde",
"white": "blanco",
"black": "negro",
"Ethan": "Yeet"
}
while 1 == 1:
tt = input("What would you like to translate? (Only write in lowercase and type EXIT to quit) ")
if str(tt) in words:
print(tt + " is " + words.get(tt, "Not in dictionary"))
elif str(tt) == "EXIT":
break
else:
atd = input("Would you like to add " + str(tt) + " to our dictionary? Y/N ")
if atd == "Y" or atd == "y":
d = input("What does it translate to? ")
d = str(d)
tt = str(tt)
words.update({tt: d})
print(tt + " is "+ words.get(tt, "What"))
continue
elif str(atd) == "EXIT":
break
else:
continue
可以使用JSON将数据存储在文件中,在打开Python文件时读取其中的数据。你知道吗
我的_脚本.py
数据.json:
相关问题 更多 >
编程相关推荐