Python Forum
Is there a something like "case" in Python 3.6?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a something like "case" in Python 3.6?
#8
(Feb-25-2017, 10:33 AM)Raptor88 Wrote: If there is some short code for the "dictionary mapping" approach that you can post, or a link to a simple explanation, would appreciate that.
Can make one more:
color = 'red' # From eg user input
if color == 'red':
   set_color = 'rgb(255,0,0)'
elif color == 'blue':
   set_color = 'rgb(0,0,255)'
elif color == 'green':
   set_color = 'rgb(0,128,0)'
else:
   print('No rgb for that color')

print(set_color) #--> rgb(255,0,0)
With dictionary and get() method.
color = 'red' # From eg user input
choices = {'red': 'rgb(255,0,0)', 'blue': 'rgb(0,0,255)', 'green': 'rgb(0,128,0)'}
set_color = choices.get(color, 'No rgb for that color')
print(set_color) #--> rgb(255,0,0)
Reply


Messages In This Thread
RE: Is there a something like "case" in Python 3.6? - by snippsat - Feb-25-2017, 11:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What colon (:) in Python mean in this case? Yapwc 4 5,325 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Switch case or match case? Frankduc 9 7,588 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 2,201 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 2,539 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to use switch/case in python? newbieguy 9 5,870 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  How to write switch case statement in Python pyhelp 8 13,006 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