Forum Archive

Proper function layout, calling

craigmrock

Trying to build a 3 function program:
1. What key(music)?
2. Major or minor?
3.What level (for practicing)?
Having trouble with local variables I think. The steps should be: get the key, decide between major or minor scales, then determine how many scales to provide, 1, 2, or 3?
I have this working properly without functions but believe there’s a cleaner way to write it.

from random import choice

def key():
    keys = ['A','B','C','D','E','F','G']
    k1=input('Choose your key: ').upper()
    if k1 in keys:
        print('Ahh,',k1, 'excellent choice.')
    else:
        print('I do not understand your choice.')

key()

def tone():
    major = ['Ionian: W,W,H,W,W,W,H','Lydian: W,W,W,H,W,W,H','Mixolydian: W,W,H,W,W,H']
    minor= ['Dorian: W,H,W,W,W,H,W','Phrygian: H,W,W,W,H,W,W','Aeolian: W,H,W,W,H,W,W','Locrian: H,W,W,H,W,W,W','Harmonic Minor: W,H,W,W,H,W+H,H']
    ask2 = input('Major or Minor: ')
    if (ask2=='major'):
            question = choice(major)
            answer=input(question).lower()

    elif (ask2=='minor'):
            question = choice(minor)
            answer=input(question).lower()

    def level():
        ask = int(input('Choose your level(1, 2, or 3): '))
        if (ask == 1):
            question = choice()
            answer=input(question).lower()
            print(answer)
        elif (ask == 2):
            question = choice(),choice()
            answer=input(question).lower()
            print(answer)
        elif (ask == 3):
            question = choice(),choice(),choice()
            answer=input(question).lower()
            print(answer)
        else:
            print('I do not understand your choice.')

    level()
tone()
JonB

It would be cleaner if those functions returned the answer, or raise an exception, e.g:

selected_key=key()
selected_tone=tone()
selected_level=level()

level() probably should not be an embedded function, but should live at the same level as key() and tone()