Python Forum
Whats the right way to refactor this Big if/elif/elif ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats the right way to refactor this Big if/elif/elif ?
#2
The first thing I think of is to make a list like so:

dists = [(forward, 'forward'), (left, 'left'), ..., (narrow_r3, 'narrow_r3')]
dists.sort()
closest_dir = dists[0][1]
But then there is the question of building the list. Could you convert your calculations into one function with parameters?

directions = (('left', [359, 0, 15]), ('right', [270, 271, 15], ...)
distances = [(dist_calc(*parameters), direction) for direction, parameters in direction]
Gribouillis likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Whats the right way to refactor this Big if/elif/elif ? - by ichabod801 - Jul-28-2019, 05:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  elif not responding on print EddieG 3 878 Jul-20-2023, 09:44 PM
Last Post: Pedroski55
Question If, elif, and else statement not working PickleScripts 3 902 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  If/elif help stupidanlearning 5 1,346 May-04-2022, 02:25 AM
Last Post: deanhystad
  Need help wioth elif michael_lwt 1 1,284 Dec-19-2021, 05:48 AM
Last Post: deanhystad
  Issue with program not passing to other elif condition Abdirahman 6 2,122 Nov-21-2021, 07:04 AM
Last Post: Abdirahman
  Help with if vs elif print_hello_world 5 3,129 Jan-07-2021, 08:50 AM
Last Post: print_hello_world
  Whats wrong with the elif? inunanimous93 3 2,485 Nov-30-2020, 03:58 AM
Last Post: deanhystad
  If, elif, else doesn't work well Vidar567 5 2,893 Nov-21-2020, 06:25 PM
Last Post: DPaul
  cant get my elif function to direct me to where i want fvfre 1 1,317 Oct-25-2020, 05:17 PM
Last Post: deanhystad
  If elif else statement not functioning SamDiggityDog 4 2,658 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020