patternpythonMinor
Phonetic Alphabet Trainer
Viewed 0 times
alphabettrainerphonetic
Problem
This is a little program I put together to help people learn the phonetic alphabet. Feel free to query the code.
import random
phonetic = { 'a' : 'alpha', 'b' : 'bravo', 'c' : 'charlie', 'd' : 'delta', 'e' : 'echo', 'f' : 'foxtrot', 'g' : 'golf', 'h' : 'hotel', 'i' : 'india', 'j' : 'juilett', 'k' : 'kilo', 'l' : 'lima', 'm' : 'mike', 'n' : 'november', 'o' : 'oscar', 'p' : 'papa', 'q' : 'quebec', 'r' : 'romeo', 's' : 'sierra', 't' : 'tango', 'u' : 'uniform', 'v' : 'victor', 'w' : 'whiskey', 'x' : 'x-ray', 'y' : 'yankee', 'z' : 'zulu' }
print('Welcome to the IkeaFish Phonetic Trainer. \n Answer no or n to stop the program.')
again = 'yes'
while again != 'no' or 'n':
#Picks either 0 or 1. If equal to 1 tests the letters, If equal to 0, tests the words.
if random.randint(0,1) == 1:
question = random.choice(list(phonetic.keys()))
answer = input('What does this letter {} stand for: '.format(question))
if answer == phonetic[question]:
print('Correct')
else:
print('Wrong answer', question, 'stands for', phonetic[question])
else:
letter = random.choice(list(phonetic.keys()))
#Grabbing a random key
question = phonetic[letter]
answer = input('What does this word {} stand for: '.format(question))
if answer == letter:
print('Correct')
else:
print('Wrong answer', question, 'stands for', phonetic[letter])Solution
Your line
is incorrect:
You have to use
or
Anyway, I replaced it with
I also did several improvements that you can see in the code, and updated the spelling to match the NATO phonetic spellings.
while again != 'no' or 'n'is incorrect:
again != 'no' or 'n' is evaluated to (again != 'no') or 'n' which is True as 'n' evaluates to True in any case.You have to use
again != 'no' and again != 'n' hereor
again not in ['no', 'n']. Anyway, I replaced it with
while True and added a break statement. This allows to exit from the loop exactly when we need it. (Otherwise we get unnecessary message that our answer is not correct.) Also your again variable never changed, so your loop in any case is infinite.I also did several improvements that you can see in the code, and updated the spelling to match the NATO phonetic spellings.
import random
phonetic = {'a': 'alfa', 'b': 'bravo', 'c': 'charlie', 'd' : 'delta',
'e': 'echo', 'f': 'foxtrot', 'g': 'golf', 'h': 'hotel',
'i': 'india', 'j': 'juliett', 'k': 'kilo',
'l': 'lima', 'm': 'mike', 'n': 'november', 'o': 'oscar',
'p': 'papa', 'q': 'quebec', 'r': 'romeo',
's': 'sierra', 't': 'tango', 'u': 'uniform', 'v': 'victor',
'w' : 'whiskey', 'x': 'x-ray', 'y': 'yankee', 'z': 'zulu'}
# made this a bit more pep8-compliant
print("Welcome to the IkeaFish Phonetic Trainer.\nAnswer 'no' or 'stop' to stop the program.")
# n can be valid answer, so it is not good to use it a stop-word
while True:
letter, word = random.choice(list(phonetic.items()))
# we can pick both elements at once
if random.choice([True, False]):
answer = input('What does this letter {} stand for: '.format(letter))
correct = word
else:
answer = input('What does this word {} stand for: '.format(word))
correct = letter
answer = answer.lower()
# make answers case-insensetive
if answer in ['no', 'stop']:
break
if answer == correct:
print('Correct')
else:
print('Wrong answer', letter, 'stands for', word)Code Snippets
while again != 'no' or 'n'import random
phonetic = {'a': 'alfa', 'b': 'bravo', 'c': 'charlie', 'd' : 'delta',
'e': 'echo', 'f': 'foxtrot', 'g': 'golf', 'h': 'hotel',
'i': 'india', 'j': 'juliett', 'k': 'kilo',
'l': 'lima', 'm': 'mike', 'n': 'november', 'o': 'oscar',
'p': 'papa', 'q': 'quebec', 'r': 'romeo',
's': 'sierra', 't': 'tango', 'u': 'uniform', 'v': 'victor',
'w' : 'whiskey', 'x': 'x-ray', 'y': 'yankee', 'z': 'zulu'}
# made this a bit more pep8-compliant
print("Welcome to the IkeaFish Phonetic Trainer.\nAnswer 'no' or 'stop' to stop the program.")
# n can be valid answer, so it is not good to use it a stop-word
while True:
letter, word = random.choice(list(phonetic.items()))
# we can pick both elements at once
if random.choice([True, False]):
answer = input('What does this letter {} stand for: '.format(letter))
correct = word
else:
answer = input('What does this word {} stand for: '.format(word))
correct = letter
answer = answer.lower()
# make answers case-insensetive
if answer in ['no', 'stop']:
break
if answer == correct:
print('Correct')
else:
print('Wrong answer', letter, 'stands for', word)Context
StackExchange Code Review Q#148030, answer score: 4
Revisions (0)
No revisions yet.