Python Forum
Is there a better way to write this case scenario?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a better way to write this case scenario?
#15
In snippet #3 you're using tuples. I haven't seen it.

Output:
In [2]: page_info(1189, 1) Out[2]: {'orientation': 'Portrait', 'size': 'Unknown'}
Then the output is right.

What I meant with my first post, is that you have to pay attention of the order in page_sizes.
Quote:With the last example you've to pay attention about the order of the definition for page_sizes.

Your example from #3 with exchanged height and width in page_sizes:
def page_info(height, width):
    if height == width:
        p_size, p_orientation = 'Unknown', 'Square'
    else:
        p_dimensions = tuple(sorted([height, width], reverse=True))
        page_sizes = {(841, 1189):'A0', (841, 594):'A1', (594, 420):'A2',
                      (420, 297):'A3', (210, 297):'A4'}
        page_orientations = {True:'Portrait', False:'Landscape'}
        p_size = page_sizes.get(p_dimensions, 'Unknown')
        p_orientation = page_orientations[height>width]
    return {'size':p_size, 'orientation':p_orientation}
 
for height, width in ((1189, 841), (210, 297), (200, 100), (300, 300)):
    p_info = page_info(height, width)
    print("Page size is {size}, orientation is {orientation}".format(**p_info))
Output:
Page size is Unknown, orientation is Portrait Page size is Unknown, orientation is Landscape Page size is Unknown, orientation is Portrait Page size is Unknown, orientation is Square
Instead of writing 10 replies, I'll post next time directly how to break your code example ;-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Is there a better way to write this case scenario? - by DeaD_EyE - Sep-26-2017, 04:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 4,886 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Help: write 'case' with Python ICanIBB 2 1,960 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to write switch case statement in Python pyhelp 9 9,619 Nov-11-2018, 08:53 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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