# Spelling Test for Lucas

# Modules
from gtts import gTTS
import os

# Global Variables
score = 0
totalquestions = 0
w1 = 'house'
w2 = 'already'
w3 = 'bathroom'
w4 = 'toilet'
w5 = 'bicycle'

# Correct Audio
correct = 'Well done! That is correct.'
language = 'en'
myobj = gTTS(text=correct, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/correct.mp3")

# Wrong Audio
wrong = 'Sorry that is incorrect.'
myobj = gTTS(text=wrong, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/wrong.mp3") 

# Introduction
print(f'Hi Lucas! Welcome to this Spelling Test.\nThis program is written by Dad in Python.\nYour current score is {score}.\n')

# Word 1
print('Listen to word 1')
w1spoken = "The first spelling word is " + w1 + ". " + w1 + ". Spell " + w1
myobj = gTTS(text=w1spoken, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/w1.mp3")
os.system("mpg123 /home/bmon/python/audio/w1.mp3")
word1 = input('Spell word 1...\n\n').lower()
totalquestions = totalquestions + 1
if word1 == w1:
    score = score + 1
    os.system("mpg123 /home/bmon/python/audio/correct.mp3")
    print(f'\nYou spelled {w1} correctly!') 
else:
    os.system("mpg123 /home/bmon/python/audio/wrong.mp3")
    print(f'\nSorry, you did not spell {w1} correctly, you typed {word1}.')
    print(f'The correct spelling is {w1}.')
print(f'Your score is now {score}/{totalquestions}.')
input("Press Enter to continue...")

# Word 2
print('\nListen to word 2')
w2spoken = "The second spelling word is " + w2 + ". " + w2 + ". Spell " + w2
myobj = gTTS(text=w2spoken, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/w2.mp3")
os.system("mpg123 /home/bmon/python/audio/w2.mp3")
word2 = input('Spell word 2...\n\n').lower()
totalquestions = totalquestions + 1
if word2 == w2:
    score = score + 1
    os.system("mpg123 /home/bmon/python/audio/correct.mp3")
    print(f'\nYou spelled {w2} correctly!') 
else:
    os.system("mpg123 /home/bmon/python/audio/wrong.mp3")
    print(f'\nSorry, you did not spell {w2} correctly, you typed {word2}.')
    print(f'The correct spelling is {w2}.')
print(f'Your score is now {score}/{totalquestions}.')
input("Press Enter to continue...")

# Word 3
print('\nListen to word 3')
w3spoken = "The third spelling word is " + w3 + ". " + w3 + ". Spell " + w3
myobj = gTTS(text=w3spoken, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/w3.mp3")
os.system("mpg123 /home/bmon/python/audio/w3.mp3")
word3 = input('Spell word 3...\n\n').lower()
totalquestions = totalquestions + 1
if word3 == w3:
    score = score + 1
    os.system("mpg123 /home/bmon/python/audio/correct.mp3")
    print(f'\nYou spelled {w3} correctly!') 
else:
    os.system("mpg123 /home/bmon/python/audio/wrong.mp3")
    print(f'\nSorry, you did not spell {w3} correctly, you typed {word3}.')
    print(f'The correct spelling is {w3}.')
print(f'Your score is now {score}/{totalquestions}.')
input("Press Enter to continue...")

# Word 4
print('\nListen to word 4')
w4spoken = "The fourth spelling word is " + w4 + ". " + w4 + ". Spell " + w4
myobj = gTTS(text=w4spoken, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/w4.mp3")
os.system("mpg123 /home/bmon/python/audio/w4.mp3")
word4 = input('Spell word 4...\n\n').lower()
totalquestions = totalquestions + 1
if word4 == w4:
    score = score + 1
    os.system("mpg123 /home/bmon/python/audio/correct.mp3")
    print(f'\nYou spelled {w4} correctly!') 
else:
    os.system("mpg123 /home/bmon/python/audio/wrong.mp3")
    print(f'\nSorry, you did not spell {w4} correctly, you typed {word4}.')
    print(f'The correct spelling is {w4}.')
print(f'Your score is now {score}/{totalquestions}.')
input("Press Enter to continue...")

# Word 5
print('\nListen to word 5')
w5spoken = "The fifth spelling word is " + w5 + ". " + w5 + ". Spell " + w5
myobj = gTTS(text=w5spoken, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/w5.mp3")
os.system("mpg123 /home/bmon/python/audio/w5.mp3")
word5 = input('Spell word 5...\n\n').lower()
totalquestions = totalquestions + 1
if word5 == w5:
    score = score + 1
    os.system("mpg123 /home/bmon/python/audio/correct.mp3")
    print(f'\nYou spelled {w5} correctly!') 
else:
    os.system("mpg123 /home/bmon/python/audio/wrong.mp3")
    print(f'\nSorry, you did not spell {w5} correctly, you typed {word5}.')
    print(f'The correct spelling is {w5}.')
print(f'Your score is now {score}/{totalquestions}.')

#End of test
result = 'The spelling test has now ended.  Your result is '
result = result + str(score) + " out of " + str(totalquestions) + ". Thank you for playing. Have a nice day Lucas."
myobj = gTTS(text=result, lang=language, slow=False)
myobj.save("/home/bmon/python/audio/result.mp3")
os.system("mpg123 /home/bmon/python/audio/result.mp3")