4
\$\begingroup\$

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])
\$\endgroup\$
1
  • \$\begingroup\$ Note that the official word for "A" is "Alfa", with "Alpha" being an acceptable variant. \$\endgroup\$ Commented Nov 25, 2016 at 0:03

1 Answer 1

4
\$\begingroup\$

Your line

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' here or 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)
\$\endgroup\$
2
  • \$\begingroup\$ I would change two if statements. Change if answer in ['no', 'stop']: to if answer.lower() in ['no', 'stop']: and change if answer == correct: to if answer.lower() == correct: This so the user can type upper or lower case but the if statements will still work. \$\endgroup\$ Commented Nov 25, 2016 at 11:49
  • \$\begingroup\$ Good point, I thought about it. Added line answer = answer.lower() to address both issues. \$\endgroup\$ Commented Nov 26, 2016 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.