データベースからディスクゴルファデータを取得するircbotを作成しています。可能なコマンドを辞書に保存したい:ユーザー入力のオプションの引数
function_dictionary = {"get PLAYERS best round from COURSE on YEAR" :
get_best_round,
"get PLAYERS THINGS from COURSE on YEAR" : get_things}
プレイヤー、コースなどはリストに保存されます。その後、
userinput = "get name1 best round from course1 on 2016"
for p in PLAYERS:
if p in userinput:
player = p
userinput = userinput.replace(p, "PLAYERS")
for c in COURSES:
if c in userinput:
course = c
userinput = userinput.replace(c, "COURSE")
for y in YEARS:
if y in userinput:
year = y
userinput = userinput.replace(y, "YEAR")
とコマンドが辞書にあるか検索します:
PLAYERS = ["name1", "name2", "name3"]
COURSES = ["course1", "course2", "course3"]
THINGS = ["birdies", "pars", "bogeys"]
YEARS = [2015, 2016, 2017]
すべての引数がユーザによって与えている場合は、私は引数を交換し、元の値を保存することができます
for key in function_dictionary:
if key in userinput:
func_to_run = function_dictionary[key]
func_to_run(player, course, year)
方法1つまたは複数の引数がユーザーの入力にない場合は、この作業を行いますか?
userinput1 = "get name1 best round"
トリガーべき:
get_best_round("name1", course = "all", year = "all")
と
userinput2 = "get best round from course1"
をトリガする必要があります。
get_best_round(name = "all", "course1", year = "all")
と
をpset = False
for p in PLAYERS:
if p in userinput:
pset=True
player = p
userinput = userinput.replace(p, "PLAYERS")
if pset==False:
player= #value for any player
をしてために繰り返し:
はジャストプレイヤー/コース/年がユーザ入力で選択されていなかったならば、お好みの値をデフォルトと言って句を追加
get_things(name = "all", "birdies", course = "all", 2016)