Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
I built a Duolingo clone using an AI coding companion

I built a Duolingo clone using an AI coding companion

I built a Duolingo clone using Amazon Q Developer in my IDE and building it was faster than I expected. Here's how.

Veliswa Boya
Amazon Employee
Published Sep 28, 2024
Last Modified Nov 1, 2024
Editor's Note: This post has been updated as a contribution to the hackathon that AWS is running through the month of November. More details on how to participate at the end of this post.
I enjoy playing educational games and quizzes are my favorite go-to. Also, I speak four languages but I've always maintained that I'm not good at learning new languages. Duolingo fascinates me because it combines gamification and help with learning a new language.
I set out to build a Duolingo clone because I thought it'd be cool to have my own Duolingo-like app. I also was curious to see if an AI coding companion could really help me build my own app faster.

Prerequisites

To get started, you need an IDE with the Amazon Q Developer plugin. I already have it installed in my VS Code IDE and you can install the plugin in your IDE by following the steps detailed here.

How I got started

I started by creating a new directory then created an empty duolingo.py file. I have my IDE open and the Amazon Q chat open on one side of the editor. To get my initial code generation I typed /dev in the Amazon Q chat followed by the following description of what I wanted.
give me the starting python code for a duolingo-like application.
Amazon Q used this description to generate the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import random

class DuolingoApp:
def __init__(self):
self.languages = ["Spanish", "French", "German", "Italian"]
self.current_language = None
self.score = 0
self.questions = {
"Spanish": [
{"question": "How do you say 'hello' in Spanish?", "answer": "hola"},
{"question": "What is the Spanish word for 'goodbye'?", "answer": "adiós"},
],
"French": [
{"question": "How do you say 'hello' in French?", "answer": "bonjour"},
{"question": "What is the French word for 'goodbye'?", "answer": "au revoir"},
],
"German": [
{"question": "How do you say 'hello' in German?", "answer": "hallo"},
{"question": "What is the German word for 'goodbye'?", "answer": "auf wiedersehen"},
],
"Italian": [
{"question": "How do you say 'hello' in Italian?", "answer": "ciao"},
{"question": "What is the Italian word for 'goodbye'?", "answer": "arrivederci"},
],
}

def start(self):
print("Welcome to the Duolingo-like App!")
self.choose_language()
self.play_game()

def choose_language(self):
print("\nChoose a language to learn:")
for i, lang in enumerate(self.languages, 1):
print(f"{i}. {lang}")

choice = int(input("Enter the number of your chosen language: "))
self.current_language = self.languages[choice - 1]
print(f"\nGreat! You've chosen to learn {self.current_language}.")

def play_game(self):
print("\nLet's start learning! Answer the following questions:")
for _ in range(5): # Ask 5 questions per session
question = random.choice(self.questions[self.current_language])
user_answer = input(f"\n{question['question']} ")

if user_answer.lower() == question['answer'].lower():
print("Correct! Well done!")
self.score += 1
else:
print(f"Sorry, that's incorrect. The correct answer is '{question['answer']}'.")

print(f"\nGame over! Your final score is: {self.score} out of 5.")

if __name__ == "__main__":
app = DuolingoApp()
app.start()
At this point it's worth highlighting that the suggestions will differ with each new project, and that the suggestions you get will be different from the ones that your friends might get. Throughout the process you can ask for more information if anything is not clear; I'll add that this ability to ask questions helped me quickly feel comfortable while using Q to build this app.
I ran the code to start testing:
Image not found
Ok, for now I want to focus on only two languages, Spanish and French, so I want to change the code to reflect this. I go back to Amazon Q Chat and with my entire code selected I type the following:
update selected code to only have Spanish and French
Q responded with a modified version of the DuolingoApp class and I pressed the Insert at cursor button to update my file with this new version.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import random

class DuolingoApp:
def __init__(self):
self.current_language = None
self.score = 0
self.questions = {
"Spanish": [
{"question": "How do you say 'hello' in Spanish?", "answer": "hola"},
{"question": "What is the Spanish word for 'goodbye'?", "answer": "adiós"},
],
"French": [
{"question": "How do you say 'hello' in French?", "answer": "bonjour"},
{"question": "What is the French word for 'goodbye'?", "answer": "au revoir"},
],
}

def start(self):
print("Welcome to the Duolingo-like App!")
self.choose_language()
self.play_game()

def choose_language(self):
print("\nChoose a language to learn:")
for i, lang in enumerate(self.languages, 1):
print(f"{i}. {lang}")

choice = int(input("Enter the number of your chosen language: "))
self.current_language = self.languages[choice - 1]
print(f"\nGreat! You've chosen to learn {self.current_language}.")

def play_game(self):
print("\nLet's start learning! Answer the following questions:")
for _ in range(5): # Ask 5 questions per session
question = random.choice(self.questions[self.current_language])
user_answer = input(f"\n{question['question']} ")

if user_answer.lower() == question['answer'].lower():
print("Correct! Well done!")
self.score += 1
else:
print(f"Sorry, that's incorrect. The correct answer is '{question['answer']}'.")

print(f"\nGame over! Your final score is: {self.score} out of 5.")

if __name__ == "__main__":
app = DuolingoApp()
app.start()
Running this new version gave me an error AttributeError: 'DuolingoApp' object has no attribute 'languages' .
To troubleshoot I asked Q for help by right-clicking while inside the file then selecting Amazon Q, and lastly selecting Fix.
Q analyzed the code and suggested that I add the self.languages list to the init**** method. Q also provided the relevant code snippet**.** Again, I pressed the Insert at cursor button to accept this code.
The error was resolved and I could now run the code successfully.
Image not found
Currently, the game is not ending as gracefully as I'd like ('Game over!' is hardly graceful lol).
Image not found

I wanted to improve it so that the user has a better experience. For this I again right-clicked then selected Amazon Q, and then Refactor.
After reviewing the suggested code, I inserted it into my file to replace the current start method. This code now included a new method _play_again which provides a simple way to determine if the user wants to continue playing or not, and the __display_goodbye_message method which has a more graceful way of ending the game.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def start(self):
self._display_welcome_message()
while True:
self.choose_language()
self.play_game()
if not self._play_again():
break
self._display_goodbye_message()

def _display_welcome_message(self):
print("Welcome to the Duolingo-like App!")

def _play_again(self):
return input("\nWould you like to play again? (yes/no): ").lower().startswith('y')

def _display_goodbye_message(self):
print("Thank you for using the Duolingo-like App. Goodbye!")
Here is how my final code looks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import random

class DuolingoApp:
def __init__(self):
self.languages = ["Spanish", "French"]
self.current_language = None
self.score = 0
self.questions = {
"Spanish": [
{"question": "How do you say 'hello' in Spanish?", "answer": "hola"},
{"question": "What is the Spanish word for 'goodbye'?", "answer": "adiós"},
],
"French": [
{"question": "How do you say 'hello' in French?", "answer": "bonjour"},
{"question": "What is the French word for 'goodbye'?", "answer": "au revoir"},
],
}

def start(self):
self._display_welcome_message()
while True:
self.choose_language()
self.play_game()
if not self._play_again():
break
self._display_goodbye_message()

def _display_welcome_message(self):
print("Welcome to the Duolingo-like App!")

def _play_again(self):
return input("\nWould you like to play again? (yes/no): ").lower().startswith('y')

def _display_goodbye_message(self):
print("Thank you for using the Duolingo-like App. Goodbye!")

def choose_language(self):
print("\nChoose a language to learn:")
for i, lang in enumerate(self.languages, 1):
print(f"{i}. {lang}")

choice = int(input("Enter the number of your chosen language: "))
self.current_language = self.languages[choice - 1]
print(f"\nGreat! You've chosen to learn {self.current_language}.")

def play_game(self):
print("\nLet's start learning! Answer the following questions:")
for _ in range(5): # Ask 5 questions per session
question = random.choice(self.questions[self.current_language])
user_answer = input(f"\n{question['question']} ")

if user_answer.lower() == question['answer'].lower():
print("Correct! Well done!")
self.score += 1
else:
print(f"Sorry, that's incorrect. The correct answer is '{question['answer']}'.")

print(f"\nGame over! Your final score is: {self.score} out of 5.")

if __name__ == "__main__":
app = DuolingoApp()
app.start()

Next steps

I had fun experimenting to see how Amazon Q Developer could help me build my own Duolingo clone. I also enjoyed interacting with the the app and learning new words from different languages. I look forward to building my next app using Amazon Q Developer and exploring even more features of Q Developer.
If you want to learn more, check out the Amazon Q Developer Dev Centre which contains lots of resources to help get you started. You can also get more Amazon Q Developer related content by checking out the dedicated Amazon Q space.
If you're ready to build your own game, AWS is running a hackathon through the month of November. From a simple tic tac toe game built with HTML/CSS/JS to a complex Unity game -- all skill levels and tech stacks are welcome. Join now on DevPost: https://awsdevchallenge.devpost.com!
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

2 Comments

Log in to comment