I want to use a dictionary to call functions with arguments, ie this simple program. I'm a very novice coder in python. what am I doing wrong? output:
what name? : s
what name? : f
f
s
want to write or read? w/r:
w
want singers or band members? s/b:
s
1
code:
def addToFile(filename, lineString):
file = open(filename,"a")
file.write(lineString + "\n")
file.close()
return 1
def readFile(filename):
file = open(filename)
for line in file:
print(line)
file.close()
return 2
whatToDo = {
"ws": addToFile("band.txt",input("what name? : ")),
"wb": addToFile("singers.txt",input("what name? : ")),
"rs": readFile("singers.txt"),
"rb": readFile("band.txt")
}
def promptWrite():
print("want to write or read? w/r: ")
choice = str(input())
print("want singers or band members? s/b: ")
choice += str(input())
val = whatToDo[choice]
print(val)
promptWrite()
I didn't know If I needed to have a value or something, so I put the returns in the functions and had val. That isn't nessisary, I just drafted this up as an example program.
I know that you can have a dictionary with the names of the functions and call
dictionaryName[whateverthing]() to run the function, but I don't know how to have the arguments vary in that
addToFile().