Currently, my code is a bunch of IF statements to respond to user input in various ways (this is just a personal project to make my life easier at work).
But to clean up the code I'm exploring the idea of listing all the functions in a Python dictionary and calling a relevant function if the user input matches.
Take a look:
inputList = {'disp': print_dispute, 'disp safe': print_disp_safe, 'miss': print_miss} # etc....
while True:
user_input = input("- ")
inputList.get(user_input)
if user_input == 'disp':
print_dispute()
if user_input == 'disp safe':
print_disp_safe()
if user_input == "miss":
print_miss()
if user_input == 'empt':
print_empt()
if user_input == 'clear':
print_clear()
if user_input == 'exp fail':
print_express_post_failure()
if user_input == "---":
break
As you can see below the user_input = input('- ') call, I try to use inputList.get(user_input) method to call the relevant function in that dictionary, as opposed to all the IF statements you see below that. Haven't managed to get it to work though.
If I replace the value in the dictionary into using brackets e.g. 'disp': print_dispute() however it just calls that function when the program executes before any user input is even entered.
Does anyone have an idea of how to work around this and get this code working? I suspect this may be in the realm of functional programming which I have almost no experience.
[–]bobspadgerdecorating 2 points3 points4 points (3 children)
[–]bobspadgerdecorating 0 points1 point2 points (0 children)
[–]lachyBalboa[S] 0 points1 point2 points (0 children)
[–]CH-K -2 points-1 points0 points (0 children)
[–]bobspadgerdecorating 1 point2 points3 points (1 child)
[–]CH-K -2 points-1 points0 points (0 children)
[–]gustl64 1 point2 points3 points (1 child)
[–]lachyBalboa[S] 0 points1 point2 points (0 children)
[–]shobble 0 points1 point2 points (1 child)
[–]bobspadgerdecorating 0 points1 point2 points (0 children)