英语转南非荷兰语翻译器匹配逻辑优化及代码哈希报错问题求助
Fixing Your English-to-Afrikaans Translator: Hash Error & Matching Logic
Hey there! Let's sort out your translator's issues—first, the hash error, then get that priority matching logic working exactly how you want.
Why the Hash Error Happens
Your line elif splitquestion in i2w: is causing the problem because splitquestion is a list (e.g., ['love', 'you'] for input "love you"), and dictionary keys need to be hashable (strings, numbers, tuples, etc.—lists aren't allowed). That's why you're seeing a hash error here.
Optimized Solution with Correct Matching Logic
Here's a revised version of your code that implements your desired priority flow (phrase first → full word → split word translation) and fixes the hash error:
# Dictionaries (note: fixed Afrikaans spelling in keys/values) i2w = {'hello': 'hallo', 'love': 'liefde', 'you': 'jy', 'I': 'ek', 'are': 'is', 'a': 'n', 'man': 'man', 'woman': 'vrou', 'how': 'hoe', 'the': 'die', 'best': 'beste', 'smell': 'reuk', 'nice': 'lekker'} i2p = {'i love you': 'ek het jou lief', 'you are welcome': 'jy is welkom', 'life is good': 'die lewe is goed'} while True: question = input("Please enter the word/phrase to translate (type 'finished' to exit): ").lower() # Exit condition if question == "finished": break # Priority 1: Match full phrase in phrase dictionary if question in i2p: print(f"The Afrikaans translation is: {i2p[question]}") continue # Priority 2: Match full input as a single word in word dictionary if question in i2w: print(f"The Afrikaans translation is: {i2w[question]}") continue # Priority 3: Split into individual words and translate each translated_words = [] for word in question.split(): # Use get() to keep original word if not found in dict translated_words.append(i2w.get(word, word)) # Join translated words into a sentence full_translation = ' '.join(translated_words) print(f"The Afrikaans translation is: {full_translation}") print("\nThanks for using the translator tool!")
Key Improvements Explained
- Fixed Hash Error: Removed the invalid check against a list, replaced with a loop over split words to process each term individually.
- Proper Priority Flow:
- First checks if the full input is a phrase in
i2p(e.g., "i love you" → "ek het jou lief"). - If not, checks if the input is a single word in
i2w(e.g., "love" → "liefde"). - If neither matches, splits the input into words, translates each (keeping the original word if no match exists), then joins them into a sentence (e.g., "love you" → "liefde jy").
- First checks if the full input is a phrase in
- Cleaner Loop: Uses
while Truewith an explicit exit condition, avoiding unnecessarybreakstatements and ensuring the tool keeps running until the user types "finished". - Spelling Fix: Corrected "Aafrikaans" to standard "Afrikaans" and fixed the article "a" to Afrikaans "'n" (a common beginner mistake!).
Testing Examples
- Input:
I love you→ Output:The Afrikaans translation is: ek het jou lief - Input:
love you→ Output:The Afrikaans translation is: liefde jy - Input:
how are you→ Output:The Afrikaans translation is: hoe is jy - Input:
finished→ Exits the tool
内容的提问来源于stack exchange,提问作者DNS Fishing
相关产品推荐
相关产品推荐

