Python Forum
How to write switch case statement in Python
Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write switch case statement in Python
#5
(May-26-2018, 01:53 PM)pyhelp Wrote: How do I use dictionary to get the same result.
You use code bye @DeaD_EyE,and add your input line as a argument to the function.
def get_weekday_number(day):
    """
    The function returns the number of weekday for given day.
    Lower case and upper case are allowed.
    """
    days = {
        'Friday': 4,
        'Monday': 0,
        'Saturday': 5,
        'Sunday': 6,
        'Thursday': 3,
        'Tuesday': 1,
        'Wednesday': 2,
        }
    return days.get(day.title(), 'Day not found')

day_of_week = input('\nInput Day of the Week as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday?\n')
print(get_weekday_number(day_of_week))
Slightly modified with get() method,
the can catch error in input and return Day not found.
Reply


Messages In This Thread
RE: How to write switch case statement in Python - by snippsat - May-26-2018, 04:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  switch case not working Username0089098 1 673 Apr-09-2023, 05:49 AM
Last Post: buran
  What colon (:) in Python mean in this case? Yapwc 4 2,052 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Switch case or match case? Frankduc 9 4,390 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  best way to use switch case? korenron 8 2,934 Aug-18-2021, 03:16 PM
Last Post: naughtyCat
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,642 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,837 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  "Switch-to-spreadsheet" entry. Feasible in Python? whatspython 2 1,980 Sep-30-2020, 01:12 PM
Last Post: buran
  How do I do this? Switch Case? mstichler 4 2,502 Jun-05-2020, 10:27 AM
Last Post: snippsat
  How to use switch/case in python? newbieguy 9 3,961 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  How to switch table area coordinates in Python Camelot and Tabula-Py john5 0 4,219 May-08-2019, 04:31 PM
Last Post: john5

Forum Jump:

User Panel Messages

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