0

i want to print string(given as input) as number(o/p) and i did it in c using switch case .i want to do the same in python...
wat if python provides switch case??n do we have equivalent of it in python??

Examle:
input:three hundred and fifty(string)
output:350(number)

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by karthik.c
0

In Python a dictionary is used to implement a switch/case statement ...

# a dictionary switch/case like statement to replace
# multiple if/elif/else statements in Python

def switch_case(case):
    return case + " --> " + {
    'one hundred' : '100',
    'two hundred' : '200',
    'three hundred and fifty' : '350'
    }.get(case, "no case available")


# test it
num_str = 'three hundred and fifty'
print(switch_case(num_str))

print(switch_case('one hundred'))

"""
my result -->
three hundred and fifty --> 350
one hundred --> 100
"""
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.