A few corrections:
- Use
key
, as outlines by iCodez
- Put
import random
above your function definition. Otherwise, a program that uses this will ask where random
is.
- Use
raw_input("What key would you like to play in")
instead of input
. If I type c
after the prompt, input
will look for a variable named c
and print its value. If that variable doesn't exist, and error is thrown. raw_input
just converts your input to a string.
- If you're defining
keyC
,keyG
, and keyE
within the function as constants, then having them as arguments to the function is not necessary (and does nothing). You could have them as optional arguments, or you could leave out the arguments entirely.
Final Code:
import random
def key(keyC=('em','f','g','am','c'),keyG=('g','c','d','em','am'),keyE=('a','b','e')):
#keyC, keyG, keyE as optional arguments
key = raw_input("What key would you like to play in")
print "I will display chords in that key"
if key == 'c':
return(random.choice(keyC))
elif key == 'g':
return(random.choice(keyG))
elif key == 'e':
return(random.choice(keyE))
else:
return "Pick another key"
Also see this answer.
Edit: As per twasbrillig, this solution assumes Python 2.x and not Python 3. I use Python 2.7, and it is still commonly seen.