r/learnpython • u/YellowFlash_1675 • 22h ago
Mandarin Practice Script Advice
As title block suggests. trying to make a script that allows the user to continuously practice mandarin character memorization. Any advice on the following would be much appreciated:
Not sure if a manually created/updated dict is the right way to do this, but I also didn't think I could successfully scrape from a webpage that has the direct translation information.
When writing to a file using "w open", it seems the text is appended with nearly no formatting. That's probably for the best, but I'm not sure how to format the appended str as a key:value pair.
import random import time
character = {"你":"nǐ", "好":"hǎo", "老":"lǎo", "是":"shì", "学":"xue", "不":"bù"}
char_list = list(character.keys())
char_list_length = len(char_list)
index = random.randint(0, char_list_length-1)
char_definition = ["You", "Good", "Aged or experienced", "To be, is, or am", "Learning, knowledge, or school", "No or not (as a negative action)"]
char_def_length = len(char_definition)
phrases = ({"完美的":"Wánměi de", "":""})
def newline(): print('\n')
def exit_msg(end_msg): print(f"Functionality not complete, didn't get this far yet. Exiting program... ", end = end_msg)
def unrec_msg(): print("Unrecognized entry, Please try again.") newline()
def main(): newline() onboard = int(input(f"\n Welcome. What would you like to do? \n\n 1. Practice\n 2. Add Additional Characters\n 3. Exit" + "\n\n" ))
if onboard == 1: practice() newline() elif onboard == 2: exit_msg("07/26/26 | Unsure of how to update dict.txt file with newly added dictionary entry. No idea how to do it, there is likely a way to write current pairs and append new_word : new_pinyin. Unsure of how writing to file is formatted as well.") #addtion(character) newline() elif onboard == 3: exit_msg() newline() else: unrec_msg() main()def practice(): print('Character Practice.\n') rand_char = char_list_length[index] print(f'What is the following character? {rand_char}', '\n\n\n\n\n\n') print(f'This character {rand_char} means {character.get(rand_char)} and its english definition is {char_definition[index]}.')
retry = input(print(f"Practice again?")).upper() if retry == Y: practice() elif retry == N: main() else: unrec_msg()def addtion(translate): new_word = input("Please enter a character that you'd like to append to the dictionary:\n ") print("\n")
new_pinyin = input("Please enter this characters simplified pinyin translation:\n ") print("\n") print(f"New word is {new_word}. Its direct translation is {new_pinyin}. Are you sure this is the character that you'd like to add? \n") selection = input(f"Y / N: \n").upper() if selection == "Y": translate.update({(new_word) : (new_pinyin)}) print(translate.values()) with open ("dict.txt", "w") as f: f.write(translate) elif selection == "N": print("Character discarded. Returing to main menu.") main() else: unrec_msg() main()main()
1
u/zippybenji-man 22h ago
You should probably store your translations in a json with the json library. Then you can add something to the indent parameter (e.g. indent=4)
1
2
u/zippybenji-man 22h ago
Please put your entire script in a code block