I am learning both Greek and Python right now, so I decided to make a simple program that quizzes you on the translations by providing it in Greek and asking for English, and if you provide the English, it will output the Greek if it is in the dictionary.
Feel free to take and modify this for your own uses if interested! It would be as simple as replacing the Greek dictionary and references with anything else. :)
import random, sys
greekTranslation = {
'Γεια σας': 'Hello',
'Καλημέρα': 'Good morning',
'Καλησπέρα': 'Good evening',
'Ευχαριστώ': 'Thank you',
'Παρακαλώ': 'Please',}
def practiceGreek():
greekPhrase = random.choice(list(greekTranslation.keys()))
print('')
print(f'What is the translation of "{greekPhrase}"?')
print('')
answer = input('Your answer: ')
if answer.strip().lower() == greekTranslation[greekPhrase].lower():
print('')
print('That is correct!')
print('')
else:
print('')
print(f'Incorrect! The correct translation is "{greekTranslation[greekPhrase]}".')
print('')
def translateToGreek():
print('')
print('What would you like to translate into Greek?')
print('')
englishPhrase = input().strip().lower()
reverseTranslation = {v.lower(): k for k, v in greekTranslation.items()}
greekPhrase = reverseTranslation.get(englishPhrase)
if greekPhrase:
print('')
print(f'The Greek phrase is: "{greekPhrase}"')
print('')
else:
print('')
print('I am sorry, I don\'t have that in my dictionary.')
print('')
while True:
print('Welcome to the Greek Practice Program!')
print('')
print('What would you like to do?')
print('')
print('[Practice] [Translate] [Exit]')
print('')
optionSelection = input().strip().lower()
if optionSelection != 'practice' and optionSelection != 'translate' and \
optionSelection != 'exit':
print('')
print('Please select either practice, translate, or exit')
optionSelection = ''
print('')
if optionSelection == 'practice':
while optionSelection == 'practice':
practiceGreek()
print('')
print('Would you like another? [yes] [no]')
print('')
selection = input().strip().lower()
if selection == 'yes':
print('')
continue
else:
print('')
break
elif optionSelection == 'translate':
while optionSelection == 'translate':
translateToGreek()
print('')
print('Would you like to translate another phrase? [yes] [no]')
print('')
selection = input().strip().lower()
if selection == 'yes':
print('')
continue
else:
print('')
break
elif optionSelection == 'exit':
print('')
print('Thank you for using the Greek Practice Program!')
print('')
sys.exit()
Nice! Some feedback (on your Python, I don’t speak Greek):
- In Python, the idiomatic way to name variables and functions is
snake_case
– for example,greekTranslation
should begreek_translation
. (EDIT after seeing your most recent reply: good luck with that when it comes to Python :) ) - You’re currently recomputing
reverseTranslation
every time the user requests an English-to-Greek translation. Unless you’re planning to modifygreekTranslation
while the program is running, it would likely be more efficient to makereverseTranslation
a global variable, computed just once at the start of the program. - The control flow in the main program could be a bit more clear:
- The condition in
while optionSelection == <'practice'/'translation'>
makes it look likeoptionSelection
could change to something else inside the loop, yet you never do so. You could just writewhile True
instead. - Instead of putting the “Please select either practice, translate, or exit” check up front, it would likely be more maintainable to add it as an
else
branch to the mainif
-elif
-elif
chain. That way, if you added in a new option, you would only have one place in the code to modify instead of two. (Also, instead ofx != a and x != b and x != c
, you could writex not in [a, b, c]
). - Speaking of the
if
-elif
-elif
chain, you could replace it with amatch
statement, which would remove the repetitions ofoptionSelection ==
.
- The condition in
Here’s how I would write the control flow for the last section:
optionSelection = input().strip().lower() match optionSelection: case 'practice': while True: practiceGreek() print('') print('Would you like another? [yes] [no]') print('') selection = input().strip().lower() print('') if selection != 'yes': break case 'translate': while True: translateToGreek() print('') print('Would you like to translate another phrase? [yes] [no]') print('') selection = input().strip().lower() print('') if selection != 'yes': break case 'exit': print('') print('Thank you for using the Greek Practice Program!') print('') sys.exit() case _: print('') print('Please select either practice, translate, or exit') print('')
(You could probably pull the
while True
loops into their own dedicated functions, actually.)Hope that helps. Feel free to ask if any of this doesn’t make sense.
Thank you for the detailed response and help! It is funny because I am actually working on learning the functions you are recommending (I am working through “Automate the boring stuff with python”) and it is literally the chapter I am on now. haha
I do have plans to modify the translation in the future, which is why I set it up the way I did. But your recommendation is a lot cleaner than what I had in mind so I likely would have had a lot of redundant code even with my plan to add later.
This helps a lot, and again thank you for taking the time on this. I appreciate it!
- In Python, the idiomatic way to name variables and functions is
Hi! Looks ok. Since you’re learning still, I recommend running this code through pylint and black. You can search for ‘python black’ and ‘python pylint’ for more details on those.
Thank you for looking it over!
I run my code using the idle python shell while I write, and then directly through my terminal when they are finished.
What benefit does running them through pylint or black offer?
They’re both “linters”. They analyze your code without executing it, known as “static analysis”, and highlight logical and stylistic errors.
Pylint will give you warnings if you try to feed a variable that holds an integer into a function that works on strings or if you deviate from python’s suggested style guide. Python doesn’t check data types until you actually run your code, so errors with data types won’t be caught until your program crashes. Using pylint allows you to get warnings before actually executing your code. Note that Python allows for type hinting which allows you to tell linters what data types variables and functions are allowed to be. Python itself ignores type hints when executing code.
Black automatically formats your code to adhere to Python’s suggested style guide. Unlike something like Pylint, it cannot be configured to ignore certain style rules. All projects using Black will have the same style. Python gives a lot of leeway in how you can format valid code, but it’s difficult for programmers to read code in dozens of different styles. Most languages have come up with their own style guide. For example, your first line does not match the suggested style. All imports should be on their own line. If you don’t want to remeber all the rules yourself, you can use Black to format your code in a way everyone is agreeable with.
Sounds to me like if someone doesn’t like my code, they have options! haha
I will likely look into things like that as I get more of my code out there, and I appreciate you taking the time to explain it.
I do see the point of the style guide, and I am learning it over time, but I will die on the hill of camelCase. haha