Monday, April 8, 2019

codingdirectional: Return a welcome message based on the input language

Hello and welcome! I think I will start another new project after the previous sport application project in next month but at this moment we are going to solve a few python related problems from Codewars starting from this chapter onward, we are just going for the easy problem because we don’t want to waste too much time thinking about the solution to each question. In this episode, we are going to solve the below problem.

Your start-up’s BA has told marketing that your website has a large audience in Scandinavia and surrounding countries. Marketing thinks it would be great to welcome visitors to the site in their own language. Luckily you already use an API that detects the user’s location, so this is an easy win.

The Task

  • Think of a way to store the languages as a database (eg an object). The languages are listed below so you can copy and paste!
  • Write a ‘welcome’ function that takes a parameter ‘language’ (always a string), and returns a greeting – if you have it in your database. It should default to English if the language is not in the database, or in the event of an invalid input.

After solving the above problem on Codewars I have made one small change to that language database because I think the author of this question has forgotten to include one of the most important languages in the world besides English and I have put that language in front of all the European languages in the database so you can easily spot which language that the author of this problem has forgotten to include. I hope you do enjoy the below python solution!

def greet(language):

    welcome = {
        'russian': 'добро пожаловать',
        'english': 'Welcome',
        'czech': 'Vitejte',
        'danish': 'Velkomst',
        'dutch': 'Welkom',
        'estonian': 'Tere tulemast',
        'finnish': 'Tervetuloa',
        'flemish': 'Welgekomen',
        'french': 'Bienvenue',
        'german': 'Willkommen',
        'irish': 'Failte',
        'italian': 'Benvenuto',
        'latvian': 'Gaidits',
        'lithuanian': 'Laukiamas',
        'polish': 'Witamy',
        'spanish': 'Bienvenido',
        'swedish': 'Valkommen',
        'welsh': 'Croeso'}

    if language in welcome.keys():
        return welcome[language]
    else:
        
        return "Welcome"

If you run the above program with one of our favorite languages, you will get the below outcome.

print(greet('spanish')) // Bienvenido

Dude, I hope you do enjoy this solution and don’t forget to come back for more!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...