2017-08-03 8 views
0
english_list = ["fire","apple","morning","river","wind"] 
spanish_list = ["fuego","manzana","mañana","río","viento"] 
english_to_spanish = dict(zip(english_list, spanish_list)) 
spanish_to_english = dict(zip(spanish_list, english_list)) 

def translate(word): 
    translation = english_to_spanish.get(word) 
    if translation: 
     return translation 

    translation = spanish_to_english.get(word) 
    if translation: 
     return translation 

    raise Exception('Word {0} does not exists'.format(word)) 

print("Welcome to the English <--> Spanish Dictionary") 
while True: 
    word = input("> ") 
    if word == 'show': 
     wordlist = input("Would you like to see the English or Spanish wordlist?") 
     if wordlist == 'english': 
      print(english_list) 
     elif wordlist == 'spanish': 
      print(spanish_list) 
    else: 
     try: 
      print(translate(word)) 
     except Exception as exeception: 
      print ("That wasn't a option") 

これは私のコードですが、私は辞書をインポートしたいと思いますが、これを行う方法がわかりません。私はかなり新しくコーディングして本当に助けが必要です。私は本当にseaminessヘルプが必要です、どんな助けも大いに感謝されるでしょう!辞書をインポートする

+1

のように行うことができますか? –

+0

インポートすると、単語のペア(英語はスペイン語とペア)のファイルがあることを意味しますか?あなたの例で暗示されているように、英語の単語とスペイン語の単語のリストを持つ2つの別々のファイルがありますか? –

答えて

0

あなたはJSONファイルから辞書をインポートする場合、インポートする辞書の*形式*である何この

import json 

json_path = 'your_json_file.json' 
with open(json_path) as json_data: 
    your_dictionary = json.load(json_data) 
関連する問題