
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.
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.
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()
update selected code to only have Spanish and French
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()
AttributeError: 'DuolingoApp' object has no attribute 'languages'
. Amazon Q
, and lastly selecting Fix
.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.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
.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!")
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()
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.